How to get specific value from room database by id?

I have a simple room database with LiveData, i want to get (cartPriceI: Double) values by id to use it within specific methods.


@Entity(tableName = "cart_table")
data class CartItemsDatabase(
    @PrimaryKey(autoGenerate = true) var cid: Int,
    @ColumnInfo(name = "titleD") var cartTitle: String?,
    @ColumnInfo(name = "imageD") var cartImage: Bitmap?,
    @ColumnInfo(name = "priceLD") var cartPriceL: Double?,
    @ColumnInfo(name = "priceID") var cartPriceI: Double?,
    @ColumnInfo(name = "itemNumD") var cartNum: Int?,
    @ColumnInfo(name = "descriptionD") var cartDes: String?): Parcelable


interface CartItemsDAO {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addItem(cartItemsDatabase: CartItemsDatabase)

    suspend fun updateCart(cartItemsDatabase: CartItemsDatabase)

    @Query("SELECT * FROM cart_table ORDER BY cid ASC")
    fun readAllData(): LiveData<List<CartItemsDatabase>>

    fun delete(cartItems: CartItemsDatabase)


class CartRepository(private val cartItemsDAO: CartItemsDAO) {
    val readAllData: LiveData<List<CartItemsDatabase>> = cartItemsDAO.readAllData()
    suspend fun addItem(cartItemsDatabase: CartItemsDatabase){
    suspend fun updateCart(cartItemsDatabase: CartItemsDatabase){


class CartViewModel(application: Application): AndroidViewModel(application) {
    val readAllData: LiveData<List<CartItemsDatabase>>
    private val repository: CartRepository

    init {
        val cartDao = AppDatabase.getDatabase(application).cartDao()
        repository = CartRepository(cartDao)
        readAllData = repository.readAllData
    fun addItem(cartItemsDatabase: CartItemsDatabase){
    fun updateCart(cartItemsDatabase: CartItemsDatabase){
        viewModelScope.launch(Dispatchers.IO) {


@Database(entities = [CartItemsDatabase::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun cartDao(): CartItemsDAO
    companion object {
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            val tempInstance = INSTANCE
            if (tempInstance != null){
                return tempInstance
                val instance = Room.databaseBuilder(
                INSTANCE = instance
                return instance

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(

I’m a beginner, i want to know if there is possible way to do that and retrieve specific values from database (ex. cartPriceI in Double) by id!

>Solution :

You can get a specific model by SELECT with WHERE statement

@Query("SELECT * FROM cart_table WHERE cid == : cid")
fun getCartItemsDatabase(cid: String): LiveData<CartItemsDatabase?>

Or you can Return a subset of a table’s columns

@Query("SELECT priceID FROM cart_table WHERE cid == : cid")
fun getCartItemPrice(cid: String): LiveData<Double>

Leave a Reply