1

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)

3
  • 2
    because when defining a method you can pass arguments as string or string array,when selection args has multiple values then handling string may be difficult but string array can hold a single values if there is only on selectionargs and multiple values if there are more Commented Mar 10, 2015 at 8:07
  • Then we could not use a single String value without array ? Commented Mar 10, 2015 at 8:10
  • its just a design choice Commented Mar 10, 2015 at 8:12

1 Answer 1

1

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 nullgets 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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your answer :) my question is about when we have only one ? and one value for it why we must pass an array for it and not a string only ! I think an String array take more space in memory instead of a string value . i hope explain it clear :)
Whoever designed this class must have decided that it's more common to have multiple where args if you're using substitutions. If you don't have multiple values, it might make more sense for you to avoid using ? altogether and place your values directly into String selection (e.g. String selection = "DefaultId=567". If you do this, just provide an empty array for the values, like this: String selectionArgs[] = new String[]{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.