문제

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