更新Android中的SQLite表条目。

huangapple go评论76阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月6日 02:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63271588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定