如何在Room @Query中使用参数字段? [英] How to use parameter fields in Room @Query?

查看:1116
本文介绍了如何在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方法的直接参数,通常是Stringint.

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屋!

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