Search code examples
androiddatabasekotlinandroid-room

Updating room database by using Object


I am using Room databse in android and I want to update it by using object.

I want to do this. Is it possible? `


@Query("UPDATE video_info_table SET videoStatus = :model.videoStatus WHERE id = :model.id")
fun delete(model: FileModelClass)

`


Solution

  • No (not directly), you should use either:-

    @Update
    fun update(model: FileModelClass)
    
    • and obviously set ALL the fields/members correctly (as all fields bar the primary key, which is used for the where clause, will be updated)

    OR

    @Query("UPDATE video_info_table SET videoStatus = :videoStatus WHERE id = :id")
    fun update(videoStatus: String, id: Long)
    
    • when calling then use thedao.update(theFileModelClass.videoStatus,theFileModel.id)

    • note that the types may have to be changed to suit the actual fields/members (e.g. perhaps Int instead of Long)

    Of course you could have a wrapper for the second in the interface and thus yes to the question but indirectly, such as

    fun update(model: FileModelClass) {
        update(model.videoStatus, model.id)
    }