1

I have the following SQLite query:

ArrayList<Long> idList = new ArrayList<>();

// adding Long values to idList

SQLiteDatabase db = getReadableDatabase();

final String whereClause = COLUMN_DEVICE_ID + " IN (?)";
final String[] whereArgs = new String[]{TextUtils.join(", ", idList)};

Cursor cursor = db.query(TABLE_DEVICES, null, whereClause, whereArgs,
    null, null, null);

Executing this query results in an empty Cursor.

It seems like the reason of the problem is the whereArgs String does not get inserted into the whereClause.

I debugged the code and noticed that the mQuery field of the Cursor is initialized to the following:

SELECT * FROM table_devices WHERE device_id IN (?)

Why is the ? not getting replaced with the whereArgs?

Thanks in advance.

justanoob
  • 1,797
  • 11
  • 27

1 Answers1

1

Solved the problem with the following:

final String args = TextUtils.join(", ", ids);
final String whereClause = String.format(COLUMN_DEVICE_ID + " IN (%s)", args);

Cursor cursor = db.query(TABLE_DEVICES, null, whereClause,
    null, null, null, null);

Although i'm not sure how safe this is compared to the ? + whereArgs approach (regarding SQL injection).

justanoob
  • 1,797
  • 11
  • 27