会议室-选择条件为IN的查询吗? [英] Room - Select query with IN condition?

查看:111
本文介绍了会议室-选择条件为IN的查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在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:

  1. 将参数作为List<String>
  2. 传递
  3. 将参数作为String[]
  4. 传递
  5. 最后将参数简单地传递为String,但格式为(value_1, value_2, ..., value_n)
  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)

尤其是最后一个应该很容易工作,因为它将(或至少应该)直接转换为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:

  1. 将参数作为List<String>
  2. 传递
  3. 将参数作为String[]
  4. 传递
  1. Passing the argument as a List<String>
  2. Passing the argument as a String[]

是可行的(您可以将String替换为数据库可以表示的任何类型,例如charint),您只需要从此更改@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆