56

How do I use the String[] selectionArgs in SQLiteDatabase.query()? I wish I could just set it to null, as I have no use for it. I am just trying to load an entire unsorted table from a database into a Cursor. Any suggestions on how to achieve this?

2
  • 1
    You can set them to null. Show your code. Commented May 8, 2012 at 21:46
  • 11
    Sometimes, the first step is to actually try something. Commented May 8, 2012 at 21:48

2 Answers 2

214

selectionArgs replace any question marks in the selection string.

for example:

String[] args = { "first string", "[email protected]" };
Cursor cursor = db.query("TABLE_NAME", null, "name=? AND email=?", args, null);

as for your question - you can use null

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

3 Comments

Thanks this worked for me. @GalBen-Haim please do you think you could give me your opinion on this question stackoverflow.com/questions/25598696/…
Note that passing null to columns is discouraged. You should explicitly query only the columns you need. developer.android.com/reference/android/database/sqlite/…
So how does it replaces ? with selection Args. Is it like first ? will be replaced by args[0]. If so what happens when there is mismatch in the num of ? and items in args[] ? . Please explain
14

Yes, you may set all parameters to null except the table name.

for example:

Cursor cursor = db.query("TABLE_NAME", null, null, null, null, null, null);

Comments

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.