Android sqlite通过调用方法更新行
我正在尝试更新我的sqlite数据库中的一行,但我找不到哪里出了问题。。。我正在使用此方法id我的DbAdapter:Android sqlite通过调用方法更新行,android,sqlite,Android,Sqlite,我正在尝试更新我的sqlite数据库中的一行,但我找不到哪里出了问题。。。我正在使用此方法id我的DbAdapter: public boolean updateRow(long rowId, String TimeDone, String ActDone) { String where = KEY_ROWID + "=" + rowId; ContentValues newValues = new ContentValues(); newValues.put(KEY
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;
}
在我的活动中,我尝试通过以下方式更新一行:
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;
}
我没有收到任何错误,但行内容更新。您是否忘记将
?
放在字符串中,其中=KEY\u ROWID+“=”+ROWID代码>
然后将其更改为String,其中=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) });
}
希望能有所帮助。此行没有为您提供正确的ID:
long rowId = spnr.getSelectedItemId();
相反,调用getSelectedItem()并从对象获取ID。尝试使用字符串where=KEY\u ROWID+“?=”+ROWID;它刚刚使用了类似的东西…请稍候,我正在输入代码…希望它能帮助您不知何故,我删除了我的评论,对不起!好的,谢谢:)不,还是一样。。。我现在没有收到错误,但它仍然没有更新行:\n我这样做了,但仍然没有错误,也没有更新。我认为这个方法做得很好,可能问题出在我的活动中,我会尝试做一些更改,但对我来说一切都很好……如果我这样做,我需要这样做:long rowId=(long)spnr.getSelectedItem();但是我在java:12-20 18:12:01.422:E/AndroidRuntime(2451):java.lang.ClassCastException:java.lang.String不能强制转换为java.lang.Long。关键是getSelectedItemId
不工作,除非您编写自己的自定义适配器覆盖getItemId
。是你干的吗?否则,您需要将ID存储在获取它们的某个位置。适配器不知道您的ID是什么。您是对的!如果我手动插入id,它会工作。。。但是我怎么才能做到你说的那样呢?因为,如果我把它放在需要强制转换的地方,我得到了错误,如果我试图将它作为字符串传递,那么它就简单得多了。老实说,我真的不能给你一个快速的答案,如何创建自定义微调器适配器。也许从这篇文章开始谢谢,我通过获取产品名称而不是ID来管理解决方案。。。该问题的快速解决方案:)