While working on a project and struggling with this very same question I found these other questions (and answers) helpful:
- Sqlite Query for multiple values in one columm
- Android/SQLite IN clause and placeholders
Here is what I've found works:
String[] args = new String[] {"A", "B"};
Cursor cur = db.query("tab1", null, "name in(?,?)", args, null, null, null);
As will:
String args = "A, B";
Cursor cur = db.query("tab1", null, "name in(" + args + ")", null, null, null, null);
So you can either use multiple ?
with the IN()
statement and match each with an element in selectionArgs
array (like the first example). If you have multiple conditions in your WHERE
clause, make sure to match the ?
with the correct element in selectionArgs
:
String[] args = new String[] {"Current", "A", "B"};
Cursor cur = db.query("tab1", null, "IsCurrent=? AND name in(?,?)", args, null, null, null);
Or you can just use a string made up of comma-delineated arguments directly in the IN()
statement in the selection
string itself (like the second example).
The referenced questions seemed to indicate that you could use a single ?
in the IN()
and somehow expand the associated parameter(s), but I was not able to get that to work.