54

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,778
  • 4
  • 19
  • 36
BenjiWiebe
  • 2,116
  • 5
  • 22
  • 41

2 Answers2

211

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,433
  • 22
  • 78
  • 131
  • 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
  • 67,549
  • 16
  • 165
  • 178