【问题标题】:How can i sum value in room database android我如何在房间数据库android中求和值
【发布时间】:2020-09-14 21:50:46
【问题描述】:

我想知道如何在房间数据库中求和值。

假设我在房间数据库中插入了这个值 (10,000),我想向数据库中插入新值 (20,000),我希望新值与旧值相加,而不是替换它。

我该怎么做?

代码示例:

数据库表:

@entity 
class sum (

id : int , 
value : Long )

Dao :

@insert (onConflict = OnConflictStrategy.REPLACE)
suspend fun insert (model :sum)

what shoud i use instead of above insert . 


@Query("SELECT * FROM sum")

fun getall () : LiveData<sum>

在活动中:

late init var : viewmodel : Viewmodeldatabase 
val textvalue : TextView

viewmodel = Viewmodelprovider(this).get(Viewmodeldatabase::class.java)

textvalue = findvidwbyid(R.id.text)
viewmodel.insert(sum(1 , 10000)

// when the above value insert , if there is an old value sum with it and not replace it or remove it 

viewmodel.getall.observe(this , Observer {

textvalue.text = it.value


)}

感谢大家观看我的代码并帮助我。

【问题讨论】:

    标签: android mysql kotlin android-room


    【解决方案1】:

    尝试在你的 dao 中添加下一个方法:

    @Query("UPDATE sum SET value = value + :addValue WHERE id =:id")
    suspend fun updateSum(id: Int, addValue: Long)
    

    然后你可以从你的 ViewModel/Activity 调用它

    更新

    对于更新/插入的单一方法,请将这些方法放在 dao 中:

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSum(model: Sum)
    
    @Query("UPDATE sum SET value = value + :value WHERE id = :id")
    suspend fun updateSum(id: Int, value: Long)
    
    @Query("SELECT * FROM sum WHERE id = :id")
    suspend fun getSumById(id: Int): Sum?
    
    @Transaction
    suspend fun updateOrInsertSum(sum: Sum) { // <-- this method updates value or insert new item, if id is not found
        getSumById(sum.id)?.let { updateSum(sum.id, sum.value) } ?: insertSum(sum)
    }
    

    当然,您也应该将方法 updateOrInsertSum 添加到您的存储库和视图模型中 然后,如果您第一次调用 id = '1',将插入值:

    viewmodel.updateOrInsertSum(Sum(1 ,10000)) // <-- id = 1, value = 10000
    

    在下一个调用项将使用相同的方法更新:

    viewmodel.updateOrInsertSum(Sum(1 ,20000)) // <-- id = 1, value = 10000+2000 
    

    【讨论】:

    • 我正在使用您上面的代码,但没有插入数据库,为什么?
    • ```@Entity (tableName = "credit") 数据类 Credit(@PrimaryKey val id : Int? , var credittext: Long )
    • ``` @Query("UPDATE credit SET credittext = credittext + :addValue WHERE id =:id") 暂停乐趣 updateSum(id: Int, addValue: Long)```
    • suspend fun update (id: Int, addValue: Long)= db.GetDao().updateSum(id , addValue)
    • ``` fun getallcredit() = db.GetDao().creditall()```
    【解决方案2】:

    在你的 Dao 中,我看到你使用了函数 getAll(), 假设您将它们保存到整数列表中

    val list: List<Int>//list holding all of you data returned in the onChanged method of the observer callback(or the lambda callback in case of Kotlin)
    

    假设您要更新位置 1 的值:

    @Update
    public void update(newVal: Int);
    

    在调用更新函数时,将来自我之前提到的“列表”的旧值和新值的总和传递给它

    database.dao.update(list[1] + newValue)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2021-05-19
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2018-08-17
      相关资源
      最近更新 更多