android搜索与where子句非法参数异常

时间:2014-09-18 21:57:30

标签: android database exception search

我尝试使用where子句并使用按钮单击事件从SQLIte数据库中检索数据。这是我使用SQLite数据库中的条件检索数据的DbControler方法

    public ArrayList<HashMap<String, String>> searchUser(String title){


    ArrayList<HashMap<String, String>> usersList;
    usersList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM books WHERE bookTitle ='?' ";

    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, new String[]{title.toString()});
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("bookAccession", cursor.getString(0));
            map.put("bookTitle", cursor.getString(1));
            map.put("bookEdition", cursor.getString(2));
            map.put("bookISBN", cursor.getString(3));
            map.put("bookType", cursor.getString(4));
            map.put("bookAvailab", cursor.getString(5));
            map.put("bookPublisher", cursor.getString(6));
            map.put("bookAuther", cursor.getString(7));
            map.put("bookcategory", cursor.getString(8));

            usersList.add(map);
        } while (cursor.moveToNext());
    }
    database.close();
    return usersList;
}

并且我在log cat

中低于错误
 java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
  at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
  at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
  at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
  at com.example.captchalib.DBController.searchUser(DBController.java:92)
  at com.example.captchalib.TestSearch$1.onClick(TestSearch.java:57)
  at android.view.View.performClick(View.java:4084)

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

2件事,首先尝试从= '?'

中删除单引号

其次,您不必执行title.toString()。它已经是一个字符串......