Search code examples
javaandroidsqliteandroid-sqlite

How to use "UPDATE Products SET Price = Price + 50 WHERE ProductID = 1" this kind of SQlite statement in android?


I want to update the a specific row of the Column.I tried to use the statement to set my value using 'db.update()'.But I cant.Is there any method which I can use to use the statement of the title to set the value of a specific row of a column?


Solution

  • Although the documentation explicitly mentions that the recommended and proper way to update a table is the method update(), your requirement can only be done with execSQL().

    Either with execSQL(String sql):

    db.execSQL("UPDATE Products SET Price = Price + 50 WHERE ProductID = 1");
    

    or with execSQL (String sql, Object[] bindArgs):

    db.execSQL(
        "UPDATE Products SET Price = Price + ? WHERE ProductID = ?",
        new String[] {"50", "1"}
    );