Fuluza
Fuluza

Reputation: 119

Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters

How do i fix this error? I am using SQLite to store my data and I have my CursorAdapter.java and DBHelper class and I am trying to use everything on the MainActivity,here is the code for the MainActivity.java

  ArrayList<ItemsHolder> array_list = new ArrayList<>();
    SQLiteDatabase db = mydb.getReadableDatabase();
    Cursor res =  db.rawQuery("select * from Todo", new String[]{COLUMN_ID});
    while(res.moveToNext()) {
        ItemsHolder itemsHolder = new ItemsHolder();
        itemsHolder.item = res.getString(res.getColumnIndex(ITEM_NAME));
        array_list.add(itemsHolder);
    }
        TodoCursorAdapter todoAdapter = new TodoCursorAdapter(this, res);
        // Attach cursor adapter to the ListView
        lvItems.setAdapter(todoAdapter);

But I keep getting the error mentioned above, what does it even mean?please help

Upvotes: 1

Views: 2540

Answers (1)

Metroids
Metroids

Reputation: 20477

Your SQL ("select * from Todo") doesn't match the number of parameters you are passing to it (new String[]{COLUMN_ID}). If you are passing arguments to db.rawQuery, you must have placeholders in the SQL you are running. For example, something like:

db.rawQuery("SELECT * FROM todo WHERE column_id = ?", new String[]{COLUMN_ID});

The error you are getting is because you are passing in a parameter to the query (COLUMN_ID), but there are no parameters (question marks) to bind the value to.

Upvotes: 1

Related Questions