简体   繁体   中英

How to update a Column in all rows with single query - Android Room Database

I am trying to update a column in Room database android which is associated with all rows.

My condition is i have a column called isActive which is a boolean column if a row contains true value for this column all other row should have false

This is what i have tried but it clearly updates a single row

@Query("UPDATE ADDRESS SET isActive = (CASE WHEN isActive = 0 THEN 1 ELSE 0 END) WHERE addressId = :addressId")

Only the row I'm pointing to with primary key is getting updated i want all rows expect that row to update

Even if the column is already true it should be changed to false

How do I solve this?

I have found a solution to the problem

This is what i have tried to replace the query with

note: we don't need 2 queries to achieve this

Answer:

@Query("UPDATE ADDRESS SET isActive = CASE addressId WHEN :addressId THEN 1 ELSE 0 END")

by including addressId in CASE have solved my problem.

Hope this might help someone!

如果要更新所有行,则必须删除 where 条件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM