lundi 25 avril 2016

Update text column based on content with a single lookup in SQLite

In my iOS and Android projects, I have a SQLite table that looks something like this:

id    words
-----------
1     apple, banana, orange, peach, strawberry
2     car, plane, orange
3     sheep, car, plane, horse, cow
.     ...
.
.

The words column is a TEXT column that holds a comma deliminated list of words.

I want to update the word list of a particular row by adding a word to the front of the list. The list should have no more than 5 items so I also would delete the last word if necessary.

For example, if I were updating row id 1 with cherry then I would get

cherry, apple, banana, orange, peach

Or if I were doing the same update on row id 2 then I would get

cherry, car, plane, orange

My Question

I know that I could do a query to get the row, process the text, and then update the row. However, this would require two table lookups, one for the query and one for the update. Is this possible to do with a single update lookup?

I know there is the replace() function but I am not replacing anything here. I'm also not simply incrementing an integer. I didn't see anything obvious in the SQLite core functions.

Aucun commentaire:

Enregistrer un commentaire