Room - Select query with IN condition?

VerumCH :

Is it possible to use SQLite's IN condition with Room?

I'm trying to select a list of items from my database where the value of a certain column (in this case a TEXT column) matches any one of a set of filter values. That's pretty easily done in SQL and SQLite, by my knowledge, just by adding an IN condition to your SELECT statement (see here). However, I can't seem to make it work with 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 have tried three different methods now:

  1. Passing the argument as a List<String>
  2. Passing the argument as a String[]
  3. And lastly passing the argument as simply a String, but formatted as (value_1, value_2, ..., value_n)

The last one in particular should work easily, as it will (or at least, it should) directly translate to SELECT * FROM Table WHERE column IN (value_1, value_2, ..., value_n), which is the exact way you would manually write out the SELECT if you were just accessing the database directly.

VerumCH :

So as I was preparing to submit this, I double-checked a bunch of the stuff I had looked up previously and found the thing I had somehow missed and would have saved this question from being necessary.

As it turns out, both of these options:

  1. Passing the argument as a List<String>
  2. Passing the argument as a String[]

are viable (and you can replace String with any type the database can represent, such as char or int), you simply need to change the syntax in the @Query annotation from this:

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

to this:

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

Easy as pie, right?

Note that the third method above (passing the argument as simply a String, but formatted as (value_1, value_2, ..., value_n)) does not appear to be supported by Room, but that's probably not a bad thing, since that's the hard way.

Since I already had the whole thing typed out, I figured I would leave the question up in case other people are have as much difficulty finding this solution as I did and stumble upon this question.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

where condition in select query

分類Dev

Modify select query with multi condition cases

分類Dev

MySQL - how to show some field in select query that accept my condition

分類Dev

how to select exact json value in mysql query condition

分類Dev

SELECT sub-query with WHERE condition in Yii2 find() / QueryBuilder

分類Dev

Select columns that satisfy a condition

分類Dev

mySQL select with condition replyTo

分類Dev

Lavarel query with condition

分類Dev

SQL query condition not working

分類Dev

MySQL query WHERE condition

分類Dev

Mongodb query - and condition

分類Dev

Room database, SELECT * FROM table by a given day

分類Dev

Room Database Query from background Thread

分類Dev

room db update multiple rows with @query

分類Dev

Android room persistence query is returning null value

分類Dev

Can sql query result be manually mapped with room?

分類Dev

Room The columns returned by the query does not have the fields

分類Dev

Mysql select by condition unless another condition is met

分類Dev

sql INSERT condition as a select statement?

分類Dev

SQL select dependent on count with condition

分類Dev

How to SELECT a value in a 'same' condition?

分類Dev

SELECT with WHERE NOT IN condition without subquery

分類Dev

Select Into #Temp From Select Query

分類Dev

Laravel concat in query (where condition)

分類Dev

React Apollo Query based on a condition

分類Dev

Mongodb $lookup with condition in aggregate query

分類Dev

Query to add quarter condition in SQL

分類Dev

Dynamic variables in select query

分類Dev

Use IF in SELECT part of query