Using case in an update statement on sqlite

I was upgrading a database on one of my apps recently when I came across a use for a 'case' statement. I had never used this on any sql database, but had seen a colleague use it on Oracle. I had a column containing string constants. I wanted to substitute this for integers. The integers in question would be the string variables assigned by android and stored in the R class. This would allow me to localize the data returned from the database very easily. In fact the Android system will do this automatically.

Here is the case statement:

update res set result = (case when  result = 'Flow' then 1 when result='Bored' then 2 when result = 'Neither' then 3 end);

I did not end up using the statement in the end as it would have involved finding out the generated values for the strings from R and then putting these in the update statement. I was not wild about this approach and so I just deleted the table and started the inserts afresh with the id data substituted in from the java code.
Still I reckon a case statement like the one above (which works :)) will prove useful at some stage.

Here by the way is the line from the holder were I take the id from the db and pass it directly to the setText of the TextView.

result.setText(c.getInt(c.getColumnIndex("result")));

I am using this bit to localize the result table and it works well.

Comments

Popular posts from this blog

AutoCompleteTextView backed with data from SQLite in Android

Building a choropleth map for Irish agricultural data

Using Statistics to Hire Developers