Domanda

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

È stato utile?

Soluzione

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" });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top