Baranidharan
Baranidharan

Reputation: 81

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?

Upvotes: 2

Views: 2162

Answers (2)

Baranidharan
Baranidharan

Reputation: 81

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!

Upvotes: 6

ravindra.kamble
ravindra.kamble

Reputation: 1023

If you want to update all the rows, then you have to remove where condition.

Upvotes: 1

Related Questions