Update some specific field of an entity in android Room

前端 未结 7 1587
臣服心动
臣服心动 2020-11-29 16:45

I am using android room persistence library for my new project. I want to update some field of table. I have tried like in my Dao -

// Method 1         


        
相关标签:
7条回答
  • 2020-11-29 17:48

    I think you don't need to update only some specific field. Just update whole data.

    @Update query

    It is a given query basically. No need to make some new query.

    @Dao
    interface MemoDao {
    
        @Insert
        suspend fun insert(memo: Memo)
    
        @Delete
        suspend fun delete(memo: Memo)
    
        @Update
        suspend fun update(memo: Memo)
    }
    

    Memo.class

    @Entity
    data class Memo (
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "title") val title: String?,
        @ColumnInfo(name = "content") val content: String?,
        @ColumnInfo(name = "photo") val photo: List<ByteArray>?
    )
    

    Only thing you need to know is 'id'. For instance, if you want to update only 'title', you can reuse 'content' and 'photo' from already inserted data. In real code, use like this

    val memo = Memo(id, title, content, byteArrayList)
    memoViewModel.update(memo)
    
    0 讨论(0)
提交回复
热议问题