Android 模糊搜索rawquery bind or column index out of range:博客分类:
今天使用了android里面的sqlite的模糊查询出错了:
android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x580608
错误代码:
- String sql = "select * from " + TABLE_USER+
- " where people_name like %?% or phone_number_text like %?% or office_number_text like %?% or family_number_text like %?%";
- String [] selectionArgs = new String[]{search_context,search_context,search_context,search_context};
- Cursor cursor = sqldb.rawQuery(sql, selectionArgs);
String sql = "select * from " + TABLE_USER+ " where people_name like %?% or phone_number_text like %?% or office_number_text like %?% or family_number_text like %?%"; String [] selectionArgs = new String[]{search_context,search_context,search_context,search_context}; Cursor cursor = sqldb.rawQuery(sql, selectionArgs);
修改之后的代码:
- String sql = "select * from " + TABLE_USER+
- " where people_name like ? or phone_number_text like ? or office_number_text like ? or family_number_text like ?";
- String [] selectionArgs = new String[]{"%" + search_context + "%",
- "%" + search_context + "%",
- "%" + search_context + "%",
- "%" + search_context + "%"};
- Cursor cursor = sqldb.rawQuery(sql, selectionArgs);
String sql = "select * from " + TABLE_USER+ " where people_name like ? or phone_number_text like ? or office_number_text like ? or family_number_text like ?"; String [] selectionArgs = new String[]{"%" + search_context + "%", "%" + search_context + "%", "%" + search_context + "%", "%" + search_context + "%"}; Cursor cursor = sqldb.rawQuery(sql, selectionArgs);
注意:我开始的时候把"%" + search_context + "%"写成了"%'" + search_context + "%'"也是错误的(多了一个')