I basically want to do a complement set operation in SQLite but not sure how.
Say I have a list of tags for an item like this in memory:
"red", "blue", "green", "yellow"
and the database has these tags associated with the same item:
"red", "orange", "purple"
What I want is to have the database reflect the state in memory. I need to:
- Delete the relationship to the tags for the item in the database that do not exist in memory. Delete “organge” and “purple.”
- Add the tag relationships I do not have in database but do in memory. Add “blue”, “green”, and “yellow”.
I could simply grab the entire list of existing tags and do the operation outside of SQL with Java (on Android), but that could be a huge list and I’m doing this on a mobile device.
I could also drop all existing tag relationships for the item and then add the ones in memory but that could potentially add a lot of relationships that already exist and seems like unnecessary work.
I basically have the following tables:
item
id, int primary key autoincrement not null
name, text
tag
id, int primary key autoincrement not null
name text
tagged_items
id, int primary key autoincrement not null
tag_id, int
item_id, int
One approach would be to have your in-memory representation hold both the current state and the last-persisted state.
Then, when it comes time to update the database, you know exactly what needs to be INSERTed and DELETEd from
tagged_items(and, possibly, INSERTed intotags), by performing set difference operations.This assumes that you either:
tagstable, since a tag should be unique already, orYou might be able to get away with holding only the tag in memory via sub-SELECTs to derive an
idfrom the tag text, but personally I’d just nuke theidcolumn fromtags.When you do the database I/O, wrap it all in a transaction, not only because logically the work should succeed or fail as a whole, but to boost performance with SQLite on Android.
Another approach would be for you to get rid of your direct SQLite access and switch to something like ORMLite that might handle this sort of thing for you. I haven’t used it personally.