Skip to content
Advertisement

Room database RawQuery() is not work on “IN” and “NOT IN” clause

I have my one table like UserTable.

JavaScript

// Now in My MainActivity.class file, I have use following code:

JavaScript

My query is running in normal database, but when I was pass array of user ids then, in @RawQuery() method of dao class is not supported for “IN” clause used in where condition “WHERE userId IN (?)”.

How, I will use “IN” clause in @RawQuery() of room database.

Advertisement

Answer

Much easier to use an @Query it’s as simple as:-

JavaScript

You’d then use getWhatever(new long[]{1L,2L})

If you need it an @rawQuery though you could do it like (used previous answer code for my convenience) :-

JavaScript

i.e. provide a CSV (although I vaguely recall being able to pass an array)


To use bind arguments (the recommended way as binding arguments protects against SQL injection) then you need a ? for each value and a corresponding array of objects.

So for 3 id’s you need IN(?,?,?) and the actual values, the bind arguments, in an Object[]. The following is an example that does this noting that it shows 2 ways of building the Object[] (the bind arguments/values):-

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement