Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use parameter fields in Room @Query?

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); 

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.

Is it possible to use an object's field in a Room @Query? If positive, so what's the proper way of referencing it.

like image 475
Michel Feinstein Avatar asked Aug 15 '18 00:08

Michel Feinstein


People also ask

What is Dao in room?

android.arch.persistence.room.Dao. Marks the class as a Data Access Object. Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class.


1 Answers

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); 
like image 182
NirmalCode Avatar answered Sep 27 '22 22:09

NirmalCode