Room - selecting a request with an IN condition?

Can I use a SQLite clause INwith Room?

I am trying to select a list of items from my database, where the value of a specific column (in this case a column TEXT) matches any set of filter values. As far as I know, this is easy to do in SQL and SQLite by simply adding a condition INto your expression SELECT( see here ). However, I cannot get it to work with the room.

I keep getting this error:

Error:(70, 25) error: no viable alternative at input 'SELECT * FROM Table WHERE column IN :filterValues'

(where the input to the DAO @Query-annotated method is called filterValues)

I tried three different methods:

  • Passing an argument as List<String>
  • Passing an argument as String[]
  • And finally, passing the argument as just a String, but formatted as(value_1, value_2, ..., value_n)

The latter, in particular, should work easily, since it (or, at least, should) be directly translated to SELECT * FROM Table WHERE column IN (value_1, value_2, ..., value_n), which is the exact way to write text SELECTmanually if you just accessed the database directly.

+4
source share
1 answer

So, when I was preparing to present this, I double-checked a bunch of things that I was looking for earlier, and found what I somehow missed, and would relieve this question of necessity.

As it turned out, both of these parameters:

  • Passing an argument as List<String>
  • Passing an argument as String[]

are viable (and you can replace Stringwith any type that the database can represent, for example charor int), you just need to change the syntax in the annotation @Query:

@Query("SELECT * FROM Table WHERE column IN :filterValues")

:

@Query("SELECT * FROM Table WHERE column IN (:filterValues)")

Easy as a pie, right?

, ( a String, (value_1, value_2, ..., value_n)), -, Room, , , , .

, , , , , , .

+7

Source: https://habr.com/ru/post/1692588/


All Articles