I want to execute a statement such as
UPDATE myTable SET myField = myField + 1
in an Android SQLite database. (Yes, I want to increment myField for all records.)
My first idea was to use execSQL, but the documentation says:
execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
…
For UPDATE statements, use any of the following instead.
• update(String, ContentValues, String, String[])
• updateWithOnConflict(String, ContentValues, String, String[], int)
However, as far as I can see, this type of UPDATE cannot be done with the SQLiteDatabase.update method. Did I miss some clever trick to do this with SQLiteDatabase.update or is the documentation broken? (Running the above SQL with execSQL works fine, although the documentation claims otherwise.)
That method is just fine. It has to be, because the underlying C API does not distinguish those. Any statement that does not return value can be executed with
execSQL.The
update* andinsert* are legitimate helper methods, but the documentation would really do better if it didn’t claim they must be used for all inserts/updates since they simply can’t be (ever usedinsert into table select ...? I do that all the time!)