I'm calling SQLite like this
String[] args = new String[]{"(A,B)"}
Cursor cur = db.query("tab1", null, "name in ?", args, null, null, null);
and receiving Exception:
android.database.sqlite.SQLiteException: near "?": syntax error: , while compiling: SELECT * FROM tab1 WHERE name in ?
How to use in
operator in query() ?
I already tried
String[] args = new String[]{"('A','B')"}
In SQLite, the INSERT INTO statement is used to add new rows of data into a table. After you create the table, this command is used to insert records into the table. You can view the output by using the SELECT statement.
Typically to get a specific row you can always request them by rowid , e.g. SELECT name FROM UnknownTable WHERE rowid = 1; However, there are some atypical situations that preclude this. You'll really want to read up on rowids to ensure that your table is going to behave as you want.
The NOT operator reverses the meaning of the logical operator with which it is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is negate operator.
String[] args = new String[]{A,B} // if A, B are variables
String[] args = new String[]{"A","B"}
Cursor cur = db.query("tab1", null, "name in (?,?)", args, null, null, null);
While working on a project and struggling with this very same question I found these other questions (and answers) helpful:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With