1

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

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
Fuluza
  • 109
  • 2
  • 13

1 Answers1

1

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.

Metroids
  • 18,999
  • 4
  • 41
  • 52
  • Hey @Metroids I did that and now I am getting this exception: java.lang.IllegalArgumentException: column '_id' does not exist – Fuluza Nov 02 '16 at 22:42