在Android中的SQLite如何更新特定的行 [英] SQLite in Android How to update a specific row
问题描述
我一直在努力,现在更新了一段特定行,似乎有两种方法可以做到这一点。从我读过,并试图,你可以使用:
I've been trying to update a specific row for a while now, and it seems that there are two ways to do this. From what I've read and tried, you can just use the:
execSQL(SQL字符串)
方法
或
更新(字符串表,ContentValues值,字符串whereClause,字符串[] whereArgs)
方法。
(让我知道这是不正确的,我是新来的Android和很新的SQL。)
(Let me know if this is incorrect as I am new to android and very new to SQL.)
因此,让我得到我的实际code。
So let me get to my actual code.
myDB.update(TableName, "(Field1, Field2, Field3)" + " VALUES ('Bob', 19, 'Male')", "where _id = 1", null);
我试图做到这一点:
I am trying to accomplish this:
更新字段1,字段2,字段3和其中的主键(_id)等于1。
Update Field1, Field2, and Field3 where the primary key (_id) is equal to 1.
Eclipse中给了我一个红色的线正下方单词更新,并给了我这样的解释:
Eclipse gives me a red line right underneath the word "update" and gives me this explanation:
在类型的方法来更新(字符串,ContentValues,字符串,字符串[]) SQLiteDatabase不适用的参数(字符串,字符串, 字符串,空)
The method update(String, ContentValues, String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, String, String, null)
我猜我无法正确分配ContentValues。任何人都可以点我朝着正确的方向?
I'm guessing I'm not assigning the ContentValues correctly. Can anyone point me in the right direction?
推荐答案
首先做一个ContentValues对象:
First make a ContentValues object :
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");
然后用更新的方法,它现在应该工作:
Then use the update method, it should work now:
myDB.update(TableName, cv, "_id "+"="+1, null);
这篇关于在Android中的SQLite如何更新特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!