Search code examples
javaandroidsqliteandroid-sqliteupdates

Android update SQLite table entry


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!


Solution

  • 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.