DaveNOTDavid
DaveNOTDavid

Reputation: 1803

How do I update columns of a Room entity only if it's null?

Is there a way to update columns of an entity only if the columns are null? Here's my Update function in my DAO interface:

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

... because as of now, a value of "null" is updated for these columns.

Upvotes: 4

Views: 2896

Answers (1)

Alexis GENET
Alexis GENET

Reputation: 31

You can try:

@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?)

Upvotes: 3

Related Questions