Android 更新表时工作不正常
我正在尝试使用以下代码,但无法更新我的表Android 更新表时工作不正常,android,sqlite,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.e
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()
,但也没有成功 在SQLiteStatement
中使用executeUpdateDelete()
方法,阅读文档 看这里
我认为如果其他所有事情都是正确的,那么问题就出在你的问题上 问题可能出在字段类型中。假设message_id不是数字,则使用like
String updateStatement = "update "+MESSAGE_TABLE+" SET status ="+i+" where message_id = '"+message_id+"'";
字段状态相同,如果不是数字类型,则必须使用“”。希望这有帮助
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();
这对我有用。
您可以根据需要更改这些值
谢谢
sHaH..如果要与executeUpdateDelete()
方法一起使用,则可以使用以下方法:
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();
注意:executeUpdateDelete()
是在API 11中引入的
:如果您附加了错误的logcat输出,它将帮助其他人诊断您的问题。在log cat中,它显示数据已更新。但当我从表中检索数据时。。。数据未更新是否确定executeUpdateDelete()的返回值为1?我没有使用
executeUpdateDelete()
。但对了,我找到了解决办法。。。谢谢你的支持。如果可能,请给我一个包含此命令示例的链接executeUpdateDelete()
。