Android 模糊搜索rawquery bind or column index out of range:博客分类:

今天使用了android里面的sqlite的模糊查询出错了:

 

android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x580608

 
错误代码:
Java代码 复制代码 收藏代码
  1. String sql  = "select * from " + TABLE_USER+  
  2.                 " where people_name like %?% or phone_number_text like %?% or office_number_text like %?% or family_number_text like %?%"
  3.         String [] selectionArgs  = new String[]{search_context,search_context,search_context,search_context}; 
  4.         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);
修改之后的代码:
Java代码 复制代码 收藏代码
  1. String sql  = "select * from " + TABLE_USER+  
  2.                 " where people_name like ? or phone_number_text like ? or office_number_text like ? or family_number_text like ?"
  3.         String [] selectionArgs  = new String[]{"%" + search_context + "%"
  4.                                                 "%" + search_context + "%"
  5.                                                 "%" + search_context + "%",  
  6.                                                 "%" + search_context + "%"}; 
  7.         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 + "%'"也是错误的(多了一个')
posted on 2013-01-18 15:01  zhengbeibei  阅读(21278)  评论(3编辑  收藏  举报