Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7008523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:44:46+00:00 2026-05-27T21:44:46+00:00

I basically want to do a complement set operation in SQLite but not sure

  • 0

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:

  1. Delete the relationship to the tags for the item in the database that do not exist in memory. Delete “organge” and “purple.”
  2. 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:44:47+00:00Added an answer on May 27, 2026 at 9:44 pm

    Say I have a list of tags for an item like this in memory… and the database has these tags associated with the same item

    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 into tags), by performing set difference operations.

    This assumes that you either:

    • get rid of the extraneous integer primary key for your tags table, since a tag should be unique already, or
    • you hold in memory both the tag and its ID

    You might be able to get away with holding only the tag in memory via sub-SELECTs to derive an id from the tag text, but personally I’d just nuke the id column from tags.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want certain parts of my window to not affect SizeToContent (things like
I basically want rsync, but don't have the luxury of being able to install
I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
So I basically want to have a static method in my AR class to
I have a 2-dimensional array of objects and I basically want to databind each
I have an SPListItemCollection. I basically want to get one of the items (randomly)
I have a (very) basic validation script. I basically want to check for any
I basically want to perform the same functions as this code, but in C.
I basically want to be able to say Hey, control give me your bitmap
I basically want to make this in my application, so our technicians have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.