Reputation: 8671
I'm using an SQLite database to store items. When I call items by their ids using where clause and selection args. If I pass a selection args array with more than one item id, it crashes with error:
Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters.
Here is how I perform the query:
override fun onCreateLoader(id: Int, args: Bundle?): Loader<Cursor> {
loaderId = id
val selectionArgs = arrayOf("2", "1")
return CursorLoader(activity!!,
MenuContract.ItemEntry.CONTENT_URI,
Constants.ITEM_PROJECTION_COLUMNS,
MenuContract.ItemEntry._ID + "=?",
selectionArgs,
null
)
}
It works fine if the selectionArgs array contains only one id. Not sure why.
Upvotes: 1
Views: 418
Reputation: 56938
The number of selection args must match the number of ?
's (placeholders). So the error is basically saying where do I put the second argument as I've already replaced the first ?
(placeholder)
It works when there is only 1 array element, because there is only 1 ?
(placeholder).
Perhaps you want :-
MenuContract.ItemEntry._ID + "=? OR " + MenuContract.ItemEntry._ID + "=?",
Then both arguments have a placeholder.
Upvotes: 1