mercredi 28 janvier 2015

Android: SQLite update Single value in a col. and then all other of the same col

I have the below table in my DB



id name is_current

1 apple 0
2 banana 1
3 mango 0
4 grapes 1
5 pineapple 1


I want to execute an update query which will update the (fruit) table last column (is_current) single value and at the same time the whole column values as well. For example the first row has an apple with id=1 and I want to set this value to 1 and all other fruit values to zero so the table look like,



id name is_current

1 apple 1
2 banana 0
3 mango 0
4 grapes 0
5 pineapple 0


Currently I am using two different queries and to different methods to achieve this


1st: is to set all values of is_current column to 0



String sql = "UPDATE "+TABLE_NAME +" SET " + is_current + " = '"+ Zero +"';


2nd: is to set the apple values to 1 by using id



String sql = "UPDATE "+TABLE_NAME +" SET " + is_current + " = '"+ One +"' WHERE "+ id + " = "+rowId;


So how can I combine these two queries to a single one to achieve this?


Aucun commentaire:

Enregistrer un commentaire