Search code examples
androidsqlsqlitesqliteopenhelper

SQLiteOpenHelper multiple record update


In my android application I am using Sqliteopenhelper to manipulate the sqlite database. I need to update a field (f1) in a table with part of another field (f2) for all records which meet a particular criteria. Which in standard sql can be written as

update table1 set f1=substr(f2,1,length(f2)-3) where f1 like 'xxx%';

Not sure how to do it in sqliteopenhelper. Thanks


Solution

  • The update method of Android's SQLiteDatabase class would require that you use ContentValues, which supports only literal values, not arbitrary SQL expressions.

    Just use execSQL:

    db.execSQL("UPDATE table1 SET f1=substr(f2,1,length(f2)-3) WHERE f1 like ? || '%'",
               new Object[] { "xxx" });