更新表无法正常工作

时间:2011-08-02 07:56:51

标签: android sqlite

我正在尝试使用以下代码,但我无法更新我的表格。

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();

我还尝试update.executeInsert()而不是update.executeI(),但它也没有用。

5 个答案:

答案 0 :(得分:1)

executeUpdateDelete()中使用SQLiteStatement方法并仔细阅读文档。 http://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html

答案 1 :(得分:1)

希望这有帮助

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();

这对我有用。 您可以根据您的要求更改值。

由于 沙..

答案 2 :(得分:0)

答案 3 :(得分:0)

我认为如果其他事情是正确的问题在你的查询中

问题可能在字段类型中。如果message_id不是数字则假设。然后使用

 String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = '"+message_id+"'";

如果字段状态不是数字类型,则必须使用''

答案 4 :(得分:0)

如果您想将SQLiteStatementexecuteUpdateDelete()方法一起使用,则可以使用:

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();

注意:API {1}}中引入了executeUpdateDelete()

See this Q&A

相关问题