I referred the following pages but still my problem is not solved,
Android sqlite update table always return 0 , SQLite in Android How to update a specific row , Android SQLite update row not working , Update Function in android SQLite is not working and SQLiteDatabase update not working?
I have a sqlite database as QUESTIONS(ID,QUES,ANS),ID is the primary key. When i try to update a value using following code no change happens in the database.
SQLiteDatabase db = null;
ContentValues values = new ContentValues();
values.put("QUES","New title");
values.put("ANS","Answer" );
long k= db.update("QUESTIONS",values,"ID=2",null);
Log.d("Success",""+k);
The value of k is 1, And there is no change in database. I also tried with
long k= db.update("QUESTIONS",values,null,null);
But no use .
Your first line sets db to be null.
You have no database configured for the UPDATE command to work on.
My problem solved
db = openOrCreateDatabase("data", Context.MODE_PRIVATE, null);
Related
I'm trying to update a table in SQLite android. I have a column called 'quantity' which stores qty of some items, say item1, item2 ...
Now when I purchase item1, I'd definitely want to 'add' the purchased qty to an existing qty of item1.
I searched the web but couldn't find a solution, hence asking this.
My simple code's below:
// This method is used to 'UPDATE' the table 'stock'.
// This method will be used by two fragments,
// 'sale' and 'purchase' fragments.
public int updateData(String cigaretteName,int quantity, int cost, int totalCost) {
// Accessing the database with writable functionality so it can be updated.
SQLiteDatabase db = this.getWritableDatabase();
// Creating content values object to put the new values in existing rows with old values.
ContentValues contentValues = new ContentValues();
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
contentValues.put(StockEntry.COLUMN_COST, cost);
contentValues.put(StockEntry.COLUMN_TOTAL_COST, totalCost);
// Which row to update, based on the cigarette name.
String selection = StockEntry.COLUMN_CIGARETTES_NAME + " LIKE ?";
String[] selectionArgs = {cigaretteName};
// Updating the table with the new values and then returning the number of rows affected.
return db.update(StockEntry.TABLE_NAME, contentValues, selection, selectionArgs);
}
This isn't working at all, now it doesn't even update the column/row.
contentValues.put(StockEntry.COLUMN_QUANTITY, (StockEntry.COLUMN_QUANTITY + quantity));
Do help guys!
I would suggest simple approach to overcome these kind of SQLite related issues.
Use SQLite Manager which is plugin for FireFox browser
Download from https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Create your dummy database there
Perform your CRUD operations here
Once everything working fine in SQLite Manager then use same query inside your project.
Above way will save your development time as well as testing.
I am working in an android studio project and I have a login activity,I created database using SharedPreferences and SQLite;I have created a table which has columns id,email,password,check_in,check_out.
I made it possible to login after I register a user;but I do not know how to insert a data in check_in cell of logged user.
So my problem is that how to make possible to add the time of check_in in table in database after logged in?
And how I can the see the table created using SQLite?
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("check_in", value);
contentValues.put("check_out", value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
I made it possible to login after I register a user;but I do not know
how to insert a data in check_in cell of logged user.
You would use the SQLiteDatabase's update method something along the lines of :-
ContentValues cv = new ContentValues();
cv.put("check_in",System.currentTimeMillis());
SQLiteDatabase db = this.getWritableDatabase();
db.update("your_table_name",cv,"email=? AND user=?",new String[]{email,user});
And how I can the see the table created using SQLite?
You can
install a product that provides this ability,
copy the database via adb or tools that utilise adb, so that a product that can look at SQLite databases can access the copied file,
extract a cursor from the table and then use this as the source for displaying the data e.g you could use something like
:-
SQLiteDatabae db = this.getWriteableDatabase();
Cursor csr = db.query("your_table_name",null,null,null,null,null,null);
Databaseutils.dumpCursor(csr);
csr.close();
I have made an SQLite database with my Titanium Application. When I use it in my application(Titanium Alloy) it goes well.
Next when I have inserted a new field into my table with an SQLite manager,the new added field data doesn't appear in App after updating my App with newer version. This problem only happened with my android device, In IOS, Its working well.
How I should do for android titanium so that when I update an App it will Also update SQlite database by fetching newly Added field from database ?? If I uninstall current App and install App again then it will work fine but I don't want to uninstall An App. I just would like to update an App. Please comment your suggestions
Android while update application SQLite database will not overwrite. It will use old SQLiteDB, If you add any new field in DB application will fetch from old SQLiteDB.
To solve this issue:
Use SQLite database version. If DB version is new means need to ALTER table in runtime.
First version :
db.execute('PRAGMA user_version = 1');
Second version :
db.execute('PRAGMA user_version = 2');
Check version before accessing table:
db.execute('PRAGMA user_version');
Another way:
Copy DB and Rename DB place it in same place in Code use new DB name. Ti.Database.open('newDBName'). This is not a good approach.
var checkDBVersion = function() {
var db = Ti.Database.open(DATABASE_NAME);
var rows = db.execute('PRAGMA user_version');
var dbVersion = 0;
if (rows.isValidRow()) {
dbVersion = rows.fieldByName('user_version');
}
db.close();
return dbVersion;};
Declare your Current DB version like var DBVersion = 2.
if(DBVersion!=checkDBVersion()){ //Write your alter query for Table }
Whenever your application DB changes for further release of application need to update your DBVersion variable.
I have around 2600 entries in my SQLite database. I need to delete all those entries and put in updated entries. I know that the database can be updated by changing the database version in the DatabaseHandler class, but that can only be done if the whole app is updated. I need to programatically update the database. Can this be done? I searched but couldn't find a satisfactory answer. Thanks.
I guess you can call a method some what like this
public void emptyDatabase() {
db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_Your_Tbl);
}
Even this will be able to delete those values quickly. Without updating the version number.
Plus as you mentioned that you have to delete the records and add new values.
It will be much faster that updating those values.
I am trying to update a single column in a SQLite database table in my Android project. What I want to do is set the value of "done" column to 1, when the vehicle number is same. I preferred to go with sql queries other than using myDB.update. So I used this query,
update 'details' set done = 1 where vehicle=="*****"
But the problem is, this query is working perfectly in the sqlite databse browser, not in android simulator. This is completely not working at the android simulator. Hope ur advice.
Thanks in advance.
//Declaration
SQLiteDatabase dbx;
ContentValues cv1;
EventDataSQLHelper eventsData;//object of class in which table is created
//on create
eventsData = new EventDataSQLHelper(this);
dbx= eventsData.getReadableDatabase();
cv1 = new ContentValues();
cv1.put("done",1);
//update query
dbx.update("details", cv1, "vehicle=" ? , null);