I am trying to use following code, but I am unable to update my table.
db = dbHelper.getWritableDatabase();
int i = 1;
String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = "+message_id;
SQLiteStatement update;
update = db.compileStatement(updateStatement);
update.executeInsert();
Instead of update.executeInsert()
, I a开发者_开发技巧lso tried update.executeI()
, but it did not work either.
Hope this helps
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
ContentView update = new ContentView();
update.put("fieldNameToUpdate","Value Here as a String " or integer);
db.update("table", update,/*where clause*/ "id=2",null);
db.close();
This works for me. you can change the values as per your requirement.
Thanks sHaH..
Use executeUpdateDelete()
method in SQLiteStatement
and read docs well. http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html
If you want to use SQLiteStatement with the executeUpdateDelete()
method, this works:
String sql = "UPDATE table_name SET column_2=? WHERE column_1=?";
SQLiteStatement statement = db.compileStatement(sql);
int id = 7;
String stringValue = "hi there";
statement.bindString(1, stringValue);
statement.bindLong(2, id); // These match to the two question marks in the sql string
int numberOfRowsAffected = statement.executeUpdateDelete();
Note: executeUpdateDelete()
was introduced in API 11. See this Q&A.
Fuller explanation on how to use prepared statements:
look here http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/
I think if every other thing is right problem is in your query
problem may be in the field type.Suppose if message_id is not a number.Then use like
String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = '"+message_id+"'";
Same for the fields status if it is not a number type have to use ''
精彩评论