Android Room:针对字符串列的查询列表项目
我有一个字符串列表:
val mylist = listOf("cat","flower")
还有一个具有字符串键入列的表,名为问题
我可以编写查询,以找到与列表项目之一完全匹配的问题:
@Query("SELECT * FROM objects WHERE question IN (:mylist)")
List<Object> queryObjects(List<String> mylist);
但是实际上,问题列数据不是单词类型,而是字符串。我需要找到列表项中的每个列表中的每个列表。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IN的使用基本上是在in子句左侧的表达式的AN =测试,以右侧的值列表。这仅考虑确切的匹配。
但是,您想要的是多个,例如,带有野生字符,每个或之间的都像 eg eg
等问题,例如'%cat当其他问题结束
时,或> 或可能是递归的公共表达式(CTE)时,例如'%flower%'等问题。前两个(喜欢或案例)可能必须通过
@rawquery
在运行时构建的类似/case子句。递归CTE选项基本上将构建单词列表(但是,如果包括空格(例如标点符号)以外的其他任何内容,可能会变得更加复杂。)
另一种选择是考虑全文搜索(FTS)。您可能希望参考
工作示例 like there emper
there是一个实现最简单的,多个喜欢的条款的示例ORS: -
对象(实体): -
alldao (daos): -
thedatabase (不使用
.allomainthreadqueries
为方便和简短): -将其全部放在一起,加载一些测试数据并运行一些提取物: -
结果
的语法错误从对象中选择 *;
>)。这是一个例子只是为了证明基本原则。The use of IN is basically an = test of the expression on the the left of the IN clause against the list of values on the right. That is only exact matches are considered.
However, what you want is multiple LIKE's with wild characters, and an OR between each LIKE e.g
question LIKE '%cat%' OR question LIKE '%flower%'
or perhapsCASE WHEN THEN ELSE END
or perhaps a recursive common table expression (CTE).The former two (LIKEs or CASEs) would probably have to be done via an
@RawQuery
where the LIKE/CASE clauses are built at run time.The Recursive CTE option would basically build a list of words (but could get further complicated if, anything other than spaces, such as punctuation marks were included.)
Another option could be to consider Full Text Search (FTS). You may wish to refer to https://www.raywenderlich.com/14292824-full-text-search-in-room-tutorial-getting-started
Working Example LIKE's
Here's an example of implementing the simplest, multiple LIKEs clauses separated with ORs:-
Objects (the Entity):-
AllDAO (the Daos):-
TheDatabase (not uses
.allowMainThreadQueries
for convenience and brevity):-Putting it all together, loading some test data and running some extracts:-
Result
SELECT * FROM objects WHERE ;
). That is the example is just intended to demonstrate the basic principle.