Balban
Balban

Reputation: 736

Updating table not working properly

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.

Upvotes: 1

Views: 1176

Answers (5)

Suragch
Suragch

Reputation: 511896

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:

Upvotes: 1

Shah
Shah

Reputation: 5020

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

Upvotes: 1

Rasel
Rasel

Reputation: 15477

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

Upvotes: 0

ahmet alp balkan
ahmet alp balkan

Reputation: 45224

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

Upvotes: 1

Related Questions