如何在Room @Query中使用参数字段? [英] How to use parameter fields in Room @Query?
问题描述
我有一个User
类,其字段为id
,所以我想使用Room运行以下查询:
I have a User
class with a field id
, so I wanted to run the following query with Room:
@Query("SELECT * FROM ticket where user_id = :user.id")
LiveData<Ticket> loadFromUser(User user);
但是我在user.id
的Android Studio上收到错误标记,并且我在网上找到的所有示例都仅使用@Query
方法的直接参数,通常是String
或int
.
But I am getting error marks on Android Studio on user.id
and all examples I find online only use the direct parameter of the @Query
method, usually a String
or an int
.
是否可以在房间@Query
中使用对象的字段?如果为正,那么引用它的正确方法是什么.
Is it possible to use an object's field in a Room @Query
? If positive, so what's the proper way of referencing it.
推荐答案
您不能将类似的参数传递给房间.它不支持完整的表达语言.您必须使用基本类型来传递参数.像这样
You can't pass parameters like that to room. It does not support a full expression language. You have to use primitive types to pass parameters. Like this,
@Query("SELECT * FROM ticket where user_id = :user_id")
LiveData<Ticket> loadFromUser(String user_id);
这篇关于如何在Room @Query中使用参数字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!