1

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 also tried update.executeI(), but it did not work either.

Jacob
  • 41,721
  • 6
  • 79
  • 81
Balban
  • 718
  • 3
  • 9
  • 24
  • If you attach a logcat output of the error it will help others diagnose your problem – Nic Strong Aug 02 '11 at 10:26
  • In log cat it is showing the data is updated. but when i retrieve data from table... the data is not updated – Balban Aug 02 '11 at 10:38
  • Are you sure the return value of executeUpdateDelete() is 1? – Nic Strong Aug 02 '11 at 10:47
  • I haven't use `executeUpdateDelete()`. But Right I got my solution... Thanks for the support. if possible please give me link which have example of this command `executeUpdateDelete()` . – Balban Aug 02 '11 at 11:09

5 Answers5

1

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:

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
1

Use executeUpdateDelete() method in SQLiteStatement and read docs well. http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
1

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..

Shah
  • 4,990
  • 10
  • 48
  • 70
0

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 ''

Rasel
  • 15,499
  • 6
  • 40
  • 50