传递字符串以用作Room查询的一部分 [英] Passing in a string to use as part of a Room query

查看:152
本文介绍了传递字符串以用作Room查询的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这在房间中不起作用?:

Why does this not work in Room?:

val dataSourceFactory =
    database.gameDao.getGames("Game.platforms LIKE '%XONE%'")

@Query("SELECT * FROM Game WHERE :likeClause")
fun getGames(likeClause: String): DataSource.Factory<Int, Game>

但是可以吗?:

@Query("SELECT * FROM Game WHERE Game.platforms LIKE '%XONE%'")
fun getGames(): DataSource.Factory<Int, Game>

有什么方法可以传递可以作为查询一部分的字符串?

Is there any way to pass in a string that can stand in as part of the query?

我知道这不是形成单个LIKE子句的正确方法,但是我实际上是在尝试传递多个LIKE子句.因此,我想要一种直接在查询中插入文本的方法,但是Room似乎不希望我这样做.

I know this isn't the correct way to form a single LIKE clause, but I'm actually trying to pass in multiple LIKE clauses. So I want a way to inject text directly into the query, but Room doesn't seem to want me to do that.

推荐答案

您在谈论动态SQL,我认为有空间是不可能的.起作用的是

You are talking about dynamic SQL and I dont think this is possible with room. what would work is

@Query("SELECT * FROM Game WHERE Game.platforms LIKE :likeClause1 AND Game.publisher LIKE :likeClause2")
fun getGames(likeClause1: String, likeClause2: String): DataSource.Factory<Int, Game>

,您可以根据需要使用ANDOR,如果要忽略其中一个like子句,只需传递一个空字符串

and you can use either AND or OR as needed and if you want to ignore one of the like clauses just pass an empty string

这篇关于传递字符串以用作Room查询的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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