会议室-选择条件为IN的查询吗? [英] Room - Select query with IN condition?
问题描述
是否可以在Room中使用SQLite的IN
条件?
Is it possible to use SQLite's IN
condition with Room?
我试图从数据库中选择一个项目列表,其中某个列(在本例中为TEXT
列)的值与一组过滤器值中的任何一个匹配.据我所知,在SQL和SQLite中很容易做到,只需在SELECT
语句中添加IN
条件即可(请参见此处).但是,我似乎无法使其与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.
我不断收到此错误:
Error:(70, 25) error: no viable alternative at input 'SELECT * FROM Table WHERE column IN :filterValues'
(其中DAO @Query
注释的方法的输入称为filterValues
)
(where the input to the DAO @Query
-annotated method is called filterValues
)
我现在尝试了三种不同的方法:
I have tried three different methods now:
- 将参数作为
List<String>
传递
- 将参数作为
String[]
传递
- 最后将参数简单地传递为
String
,但格式为(value_1, value_2, ..., value_n)
- Passing the argument as a
List<String>
- Passing the argument as a
String[]
- And lastly passing the argument as simply a
String
, but formatted as(value_1, value_2, ..., value_n)
尤其是最后一个应该很容易工作,因为它将(或至少应该)直接转换为SELECT * FROM Table WHERE column IN (value_1, value_2, ..., value_n)
,如果您只是访问SELECT
,则这是您手动写出SELECT
的确切方法.直接建立数据库.
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.
推荐答案
因此,当我准备提交此文件时,我仔细检查了我以前查过的一堆东西,发现我以某种方式错过了该东西,并且会已经避免了这个问题的必要性.
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:
- 将参数作为
List<String>
传递
- 将参数作为
String[]
传递
- Passing the argument as a
List<String>
- Passing the argument as a
String[]
是可行的(您可以将String
替换为数据库可以表示的任何类型,例如char
或int
),您只需要从此更改@Query
批注中的语法:>
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")
对此:
@Query("SELECT * FROM Table WHERE column IN (:filterValues)")
像馅饼一样容易,对吧?
Easy as pie, right?
请注意,上面的第三个方法(将参数简单地传递为String
,但格式为(value_1, value_2, ..., value_n)
)似乎不受Room的支持,但这可能不是一件坏事,因为这是很难的方法.
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.
这篇关于会议室-选择条件为IN的查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!