Android SQLite: Update Statement
I need to implement SQLite in my application. I followed this tutorial.. Creating and using databases in Android one
Everything is working fine. I inserted 1 row with 5 columns. Now I want to update value of 1 column only and others will be remain same.
There is one update method in tutorial but it needs all the parameters, however I want to update only one column.
Solution 1:
You can use the code below.
String strFilter = "_id=" + Id;
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
myDB.update("titles", args, strFilter, null);
Solution 2:
You can try:
db.execSQL("UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=6 ");
Or
ContentValues newValues = new ContentValues();
newValues.put("YOUR_COLUMN", "newValue");
db.update("YOUR_TABLE", newValues, "id=6", null);
Or
ContentValues newValues = new ContentValues();
newValues.put("YOUR_COLUMN", "newValue");
String[] args = new String[]{"user1", "user2"};
db.update("YOUR_TABLE", newValues, "name=? OR name=?", args);
Solution 3:
It's all in the tutorial how to do that:
ContentValues args = new ContentValues();
args.put(columnName, newValue);
db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null);
Use ContentValues
to set the updated columns and than the update()
method in which you have to specifiy, the table and a criteria to only update the rows you want to update.