Android fails to call SQLite WHERE column LIKE '%?%' The statement has 0 parameters

佐手、 提交于 2019-12-08 01:24:31

问题


With this code

public static List<App> findByKeyword(Context context, String keyword){
   ContentResolver resolver = context.getContentResolver();
   Uri uri = getContentUri();
   String[] projection = DataColumns.ALL;
   String selection = DataColumns.NAME+" like '%?%' ";
   String[] selectionArgs = {keyword};
   Cursor cursor = resolver.query(uri, projection, selection, selectionArgs, DEFAULT_ORDER );
   return cursorToList(cursor);
}

I get error

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.

The SQLite raw query should be SELECT * FROM table WHERE name LIKE '%?%' but there is problem of using parameter though ContentResolver query API

I also tried using " quotes

String selection = DataColumns.NAME+" like \"%?%\" ";

It also says

The statement has 0 parameters.


回答1:


Try the other way round:

String selection = DataColumns.NAME+" like ? ";
String[] selectionArgs = new String[]{ "%"+keyword+"%" };

hope it helps



来源:https://stackoverflow.com/questions/25932519/android-fails-to-call-sqlite-where-column-like-the-statement-has-0-paramet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!