简体   繁体   中英

android search with where clause illegal argument exception

Im trying to retrieve data from the SQLIte database with a where clause and using a button click event. This is my DbControler method to retrieve data with a condition from the SQLite database

    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;
}

and Im getting below Error in 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)

Please help me with this

2 things, First try removing the single quotes from = '?'

Second, you don't have to do title.toString() . It is already a string...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM