【发布时间】: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