我正在尝试更新我的sqlite数据库中的一行,但我无法找出问题所在...我正在使用此方法id我的DbAdapter:
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
在我的Activity中,我尝试通过执行以下操作来更新行:
String a = "20:34:23";
String item2 = spnr.getSelectedItem().toString();
long rowId = spnr.getSelectedItemId();
ContentValues newValues = new ContentValues();
newValues.put("Time", a);
newValues.put("Activity",item2);
myDb.updateRow(rowId, a, item2);
我忘了什么,或者我做错了什么?
提前致谢!
编辑: 我试过这种方式;
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
String where = KEY_ROWID + "= ?" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
// Insert it into the database.
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
但我收到以下错误:
12-20 17:45:12.521: E/AndroidRuntime(1979): android.database.sqlite.SQLiteException: variable number must be between ?1 and ?999 (code 1): , while compiling: UPDATE MainTable SET Activity=?,Time=? WHERE _id= ?0
如果我这样做:
public boolean updateRow(long rowId, String TimeDone, String ActDone) {
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
return db.update(DATABASE_TABLE,newValues, KEY_ROWID + " = ?",
new String[] { String.valueOf(rowId) })!= 0;
}
我没有错误,但行剂量更新。
答案 0 :(得分:0)
您是否忘记将?
放入String where = KEY_ROWID + "=" + rowId;
然后将其更改为String where = KEY_ROWID + "= ?" + rowId;
或尝试这样的事情:
public int updateRow(long rowId, String TimeDone, String ActDone) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues newValues = new ContentValues();
newValues.put(KEY_TIME, TimeDone);
newValues.put(KEY_ACT, ActDone);
return db.update(DATABASE_TABLE,newValues, KEY_ROWID + " = ?",
new String[] { String.valueOf(rowID) });
}
希望它可能会有所帮助。
答案 1 :(得分:0)
此行没有为您提供正确的ID:
long rowId = spnr.getSelectedItemId();
相反,调用getSelectedItem()并从对象中获取您的ID。