Android Sqlite数据库查询操作使用 '%?%' 的问题

Sqlite数据库查询操作使用 '%?%' 的问题,


public static final String QUERY_DREAM_BY_CONDITION = "select * from Journal where description like ’%?%‘ ";

Cursor cursor = db.rawQuery(ConstantUtil.QUERY_DREAM_BY_CONDITION, new String[]{condition}); // condition为查询条件


查询时会出错,log如下:

02-15 16:41:34.633: E/AndroidRuntime(10789): Uncaught handler: thread main exiting due to uncaught exception
02-15 16:41:34.803: E/AndroidRuntime(10789): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x4dbad8


具体原因好像是编码的问题:

参考资料:

http://www.eoeandroid.com/forum.php?mod=viewthread&tid=2172

http://www.eoeandroid.com/thread-27411-1-1.html


解决办法,用两个 %%,且不能用在sql语句中 (select * from Journal where description like ’%%?%%‘ 这也是不行的)


public static final String QUERY_DREAM_BY_CONDITION = "select * from Journal where description like ?";

Cursor cursor = db.rawQuery(ConstantUtil.QUERY_DREAM_BY_CONDITION, new String[]{"%%"+condition+"%%"});

你可能感兴趣的:(android SQLite)