Vishal Patoliya ツ
Vishal Patoliya ツ

Reputation: 3238

How to execute query from variable in Room Database in Query Anotation

1.Here is my MessageDao interface

 @Dao public interface MessageDao {
            @Query(Constants.QUERY.query)
            List<Message> getAllMessages();
    }

2.From Activity i want to pass whole query for getting messages.

String query = "select message from message where "+ whereLike + "group by message.messageId";

How can i execute query variable inside query anotation.

Upvotes: 2

Views: 1016

Answers (1)

ashish bulchandani
ashish bulchandani

Reputation: 76

@Dao public interface MyDao {
@Query("SELECT * FROM user WHERE age BETWEEN :minAge AND      :maxAge")
public User[] loadAllUsersBetweenAges(int minAge, int maxAge);

@Query("SELECT * FROM user WHERE first_name LIKE :search "
       + "OR last_name LIKE :search")
public List<User> findUserWithName(String search);}

The values in the queries above are prefixed with a colon, which means that these values will be supplied to the query as parameters to the method with which the query is associated. Room maps these values inside the query to the passed parameter inside the method by name. And, if Room is unable to find matching named parameters in the methods, then a compile time error gets thrown.

Upvotes: 2

Related Questions