Room @Query注释字符串字段,不带引号

时间:2019-04-16 09:42:23

标签: database sqlite android-room androidx

我有这样的DAO方法:

@Query("SELECT * FROM Libs where :field like :search")
fun findLib(search:String,field:String): List<Lib>

我想原样替换 field ,但Room在生成的代码中为其添加了引号。 例如,我想要findLib(“ james%”,“ author”)生成此SQL:

SELECT * FROM Libs where author like "james%"

但它会生成此内容:

SELECT * FROM Libs where "author" like "james%" 

如何强制它不插入引号?

1 个答案:

答案 0 :(得分:1)

据我所知,

SQLite不支持列名的参数。因此,您正在滥用房间。正确的用例是这样:

@Query("SELECT * FROM Libs WHERE author LIKE :search")
fun findLib(search:String): List<Lib>

用法:findLib(“ james%”)