kotlin 如何通过id从房间数据库中获取具体值?

vfh0ocws  于 2023-01-31  发布在  Kotlin
关注(0)|答案(2)|浏览(134)

我有一个带有LiveData的简单房间数据库,我想获取(cartPriceI:Double)值,以便在特定方法中使用它。
CartItemsDatabase.kt

@Parcelize
@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

CartItemsDAO.kt

@Dao
interface CartItemsDAO {

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

    @Update
    suspend fun updateCart(cartItemsDatabase: CartItemsDatabase)

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

    @Delete
    fun delete(cartItems: CartItemsDatabase)
}

CartRepository.kt

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

CartViewModel.kt

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){
        viewModelScope.launch(Dispatchers.IO){
            repository.addItem(cartItemsDatabase)
        }
    }
    fun updateCart(cartItemsDatabase: CartItemsDatabase){
        viewModelScope.launch(Dispatchers.IO) {
            repository.updateCart(cartItemsDatabase)
        }
    }
}

AppDatabase.kt

@Database(entities = [CartItemsDatabase::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun cartDao(): CartItemsDAO
    companion object {
        @Volatile
        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
            }
            synchronized(this){
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "cart_database"
                ).build()
                INSTANCE = instance
                return instance
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(
                context.applicationContext,
                AppDatabase::class.java,
                "cartItems_database"
            )
                .build()
        }
    }
}

我是一个初学者,我想知道是否有可能的方法来做到这一点,并从数据库中检索特定的值(如cartPriceI在双)的id!

hrysbysz

hrysbysz1#

您可以通过给定的id查询数据库,并使用suspend函数或LiveData/Flow异步返回
CartItemsDAO

@Query("SELECT cartPriceI FROM cart_table WHERE cid = :id")
suspend fun getPrice(id: Int): Double

@Query("SELECT cartPriceI FROM cart_table WHERE cid = :id")
fun getPrice(id: Int): LiveData<Double>
w51jfk4q

w51jfk4q2#

通过WHERE语句,可以通过SELECT得到特定的模型

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

也可以返回表列的子集

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

相关问题