Reputation: 1528
I answered a question in stack overflow and the user that asked the question ask me:
SQlite Query in android using cursor
why i must give my selectionArgs an Array instead its a single value ?
I didn't think about this before and its my question too now.
We must use an array when we have only one value for selectionArgs
in DB query?
(I hope i right my question in right place)
Upvotes: 1
Views: 1560
Reputation: 9673
Here's the relevant documentation. Basically, String[] selectionArgs
is an array so you can have one or more arguments for your sql WHERE clause. Since these parameters are used in the database.query()
method, they're actually building a raw sql query string to be run by SQLite. In the String selection
parameter, you specify all the WHERE clause column names, like this:
String selection = "DefaultId =? OR Name=?";
For every ?
in selection
, a corresponding WHERE value is substituted from your selectionArgs
array. So, you should have something like this:
String[] selectionArgs = new String[]{"567", "Bob"};
The database.query()
method iterates through selectionArgs
for every ?
it finds in selection
. So, the array size should match the number of ?
placeholders you're using in selection
. (If there are more ?
than selectionArgs
values, then I believe null
gets substituted instead. I haven't verified this though.)
So basically, since it's possible to have multiple WHERE values substituted into selection
, that's why selectionArgs
is an array instead of a simple String.
Upvotes: 1