如果Room实体的列为空,我该如何更新它?

时间:2018-03-14 17:54:44

标签: android android-sqlite android-room android-database android-architecture-components

是否有办法更新实体的列仅当列为空时?这是我的DAO界面中的更新功能:

@Query("UPDATE media SET media_name = :mediaName, media_data = :mediaData WHERE id = :id")
fun update(id: Int, mediaName: String?, mediaData: String?)

...因为截至目前,值为" null"已针对这些列进行更新。

1 个答案:

答案 0 :(得分:1)

您可以尝试:

@Query("UPDATE media SET media_name = (CASE WHEN media_name IS NULL THEN :mediaName ELSE media_name END), media_data = (CASE WHEN media_data IS NULL THEN :mediaData ELSE media_data END) WHERE id = :id")
fun update(id: Int, mediaName: String?, mediaData: String?)