Search code examples
androidandroid-cursor

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


I got the error in below code. What's the root cause?

Cursor cursor = context.getContentResolver().query(
                TaskEntry.CONTENT_URI,
                null,
                TaskEntry.COLUMN_TASK_STATUS + "=?",
                new String[]{TaskStatus.NEW.name(), TaskStatus.FINISHED.name(), TaskStatus.DELETED.name()},
                null
        );

Solution

  • What's the root cause?

    your where clause/string TaskEntry.COLUMN_TASK_STATUS + "=?", has only one "placeholder", =?, but you are providing three different pieces of information for the substitution,

    new String[]{TaskStatus.NEW.name(), TaskStatus.FINISHED.name(), TaskStatus.DELETED.name()}

    the number of ? has to match the size of the String's array. So in your case it should be only one. As pointed out by @Gabe, you can either use IN or OR if you want to fetch the columns with one of those values