英文:
Android update SQLite table entry
问题
我有一个本地的SQLite数据库在我的应用程序中。我创建并打开数据库如下:
CookieClickerBase = getBaseContext().openOrCreateDatabase(CookieBase, MODE_PRIVATE, null);
CookieClickerBase.execSQL("CREATE TABLE IF NOT EXISTS cookiedata(what TEXT, data LONG)");
我向表中插入一些数据,像这样:
CookieClickerBase.execSQL("INSERT INTO cookiedata VALUES ('Image','3')");
但现在我想要将表中what字段为"Image"的数据从3更改为9。我该如何做?
谢谢!
英文:
I have a local SQLite Database in may App. I create and open the Database like this:
CookieClickerBase = getBaseContext().openOrCreateDatabase(CookieBase, MODE_PRIVATE, null);
CookieClickerBase.execSQL("CREATE TABLE IF NOT EXISTS cookiedata(what TEXT, data LONG)");
I insert some thin into the table link this:
CookieClickerBase.execSQL("INSERT INTO cookiedata VALUES ('Image','3')");
But now I wont to change the data from 3 to 9 in the table entry, where what = Image.
How can I do that?
THANKS!
答案1
得分: 1
你可以使用execSQL()
语句进行更新操作:
CookieClickerBase.execSQL("UPDATE cookiedata SET data = 9 WHERE what ='Image'");
或者使用推荐的方法,即使用update()
和ContentValues
:
ContentValues cv = new ContentValues();
cv.put("data", "9");
int rows = CookieClickerBase.update("cookiedata", cv, "what = ?", new String[] {"Image"});
变量rows
将包含已更新的行数。
英文:
You could use an UPDATE
statement with execSQL()
:
CookieClickerBase.execSQL("UPDATE cookiedata SET data = 9 WHERE what ='Image'");
or use the recommended method which is update()
with ContentValues
:
ContentValues cv = new ContentValues();
cv.put("data", "9");
int rows = CookieClickerBase.update("cookiedata", cv, "what = ?", new String[] {"Image"});
The variable rows
will contain the number of updated rows.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论