我一直在尝试更新特定行一段时间,似乎有两种方法可以做到这一点。根据我的阅读和尝试,您可以使用:
execSQL(String sql)
方法
或:
update(String table, ContentValues values, String whereClause, String[] whereArgs)
方法。
(如果这不正确,请告诉我,因为我是 android 新手并且对 SQL 非常陌生。)
那么让我来看看我的实际代码吧。
myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);
我正在努力做到这一点:
更新主键 (_id) 等于 1 的 Field1、Field2 和 Field3。
Eclipse 在“更新”这个词的正下方给了我一条红线,并给了我这样的解释:
The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, String, String, null)
我猜我没有正确分配 ContentValues。谁能指出我正确的方向?
最佳答案
首先制作一个 ContentValues 对象:
ContentValues cv = new ContentValues();
cv.put("Field1","Bob"); //These Fields should be your String values of actual column names
cv.put("Field2","19");
cv.put("Field2","Male");
然后使用更新方法,它现在应该可以工作了:
myDB.update(TableName, cv, "_id = ?", new String[]{id});
关于java - Android中的SQLite如何更新特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798473/