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

有帮助吗?

解决方案

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" });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top