Android content provider query IN clause

Viewed 5095

Is possible to use an IN clause for a content provider?

I am currently using

cursor = contentResolver.query(CONTENT_URI, PROJECTION, "field IN (?)", new String[] { argsBuilder.toString() }, null);

If i remove the (?) and just use ? I get an error.

I get 0 count in my cursor.

If I type manually and execute the query in sqlite3 it works.

Help?

3 Answers

This is a code snippet from my application built in Kotlin, the params array is for illustration purposes only as the parameters come from elsewhere in the code.

The problem with most of the proposed solutions is that they result in the loss of the SQL injection prevention that the use of placeholders provides, so I only use an expression that returns an array of "?" and whose quantity matches the array of provided parameters and then becomes a comma separated String.

That way, the delivered string is a string of placeholders and not the parameters directly.

Sorry for my first answer write in spanish.

var params = arrayOf("1789","1787","1694","1784")
applicationContext.contentResolver.query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    null,
   "_ID IN (" +  Array(params.size) { "?" }.joinToString() + ")",
    params,
    null
)
Related