简体   繁体   English

如何使用单个查询更新所有行中的列 - Android Room Database

[英]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.我正在尝试更新与所有行相关联的Room数据库 android 中的一列。

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我的条件是我有一个名为isActive的列,它是一个boolean列,如果一行包含该列的true值,所有其他行都应该有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注意:我们不需要 2 个查询来实现这一点

Answer:回答:

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

by including addressId in CASE have solved my problem.通过在 CASE 中包含 addressId 解决了我的问题。

Hope this might help someone!希望这可以帮助某人!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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