Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use where in @query in room library in Android

I am using Room persistence library in my to access DB data.

In my dao class i write queries as

@Query("SELECT id, uniqueId, number, user_id, first_name, last_name,
           image FROM CONTACTS WHERE status != stts")
    abstract fun allContacts(stts: Int): DataSource.Factory<Int, Contact>

This is fine . But what if i have to compare status to some constant value , like status != Constant.STATUS_FAILED

Here val STATUS_FAILED = 5 defined in Constant File.

How do i use it in query. I know i can just pass it in the allContacts method as parameter and use like status != :stts . But i do not want that. Is there any way to use it directly like status != Constant.STATUS_FAILED.

I do not see any example on https://developer.android.com/training/data-storage/room/accessing-data

like image 254
Pardeep Kumar Avatar asked Nov 01 '25 03:11

Pardeep Kumar


2 Answers

You must add this : in @Query

@Query("Select * from Workout where nameTypeTraining= :nameTypeTraining")

  • getAllWorkout(String nameTypeTraining);
like image 190
AlexPad Avatar answered Nov 03 '25 13:11

AlexPad


Try it like this

@Query("SELECT id, uniqueId, number, user_id, first_name, last_name,
       image FROM CONTACTS WHERE status != " + Constant.STATUS_FAILED)
abstract fun allContacts(): DataSource.Factory<Int, Contact>
like image 41
Aleksandr Melnikov Avatar answered Nov 03 '25 12:11

Aleksandr Melnikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!