52

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?

Razor
  • 1,708
  • 3
  • 16
  • 35
BenjiWiebe
  • 1,955
  • 3
  • 21
  • 39

2 Answers2

207

selectionArgs replace any question marks in the selection string.

for example:

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

as for your question - you can use null

Gal Ben-Haim
  • 17,068
  • 21
  • 75
  • 129
  • Thanks this worked for me. @GalBen-Haim please do you think you could give me your opinion on this question http://stackoverflow.com/questions/25598696/recommended-way-order-to-read-data-from-a-webservice-parse-that-data-and-inse – Axel Sep 02 '14 at 03:21
  • Note that passing null to `columns` is discouraged. You should explicitly query only the columns you need. http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html – karl Mar 22 '16 at 21:38
  • 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 – Rohit Singh Feb 12 '18 at 07:02
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);
waqaslam
  • 66,380
  • 16
  • 161
  • 174