1

I'm looking for a way to fill/update my SQLiteDatabase table. On some occasions I need to be notified (by certain return value). Each row contains a url of a file that needs to be downloaded later.

  1. row does not exist yet: insert it and notify me that this row should be listed for download.
  2. row does exist but the one I'm trying to insert contains the same values as the one already in the db: do nothing, do not notify me that this should be downloaded.
  3. row does exist and the one I'm trying to insert contains different values as the one already in the db: replace this row, notify me that this should be downloaded.

I've been looking for the answer but can't seem to find something good. Should I use insertWithOnConflict(), replace(), replaceOrThrow(), ...

Thanks!

4
  • Why not simply perform a query first and then insert or update depending on the result? Commented Oct 2, 2013 at 9:59
  • perhaps look for triggers... Commented Oct 2, 2013 at 10:00
  • @ThijsMergaert I'm hoping to find a way that handles this as quick as possible. It's possible that I'll have about 1000 rows each time Commented Oct 2, 2013 at 10:03
  • 1
    @dumazy The closest thing to what you're trying to do is using insertWithOnConflict() with CONFLICT_REPLACE as conflict algorithm, combined with a column with a UNIQUE constraint, but that will only respect the 1st and 3rd cases of the ones you listed. In the 2nd case, it will delete the old row and insert a new one anyway, at least that's how I read the documentation. See link. But even that mentions that its behaviour might change in the future. Commented Oct 2, 2013 at 11:56

2 Answers 2

2

Do it in two steps wrapped in a single sqlite transaction:

  1. Query by rowid. If no rowid or or no result found, insert new and download.

  2. Compare url of new row and row retrieved in step 1. Only if the urls are different, update the row and download, otherwise no-op.

Sign up to request clarification or add additional context in comments.

2 Comments

Might this cause performance problems if I have about a 1000 rows?
If you're worried about performance, bundle up multiple query-inserts in the same transaction (say, 100 in each) to spend less time waiting for I/O.
0

First, you query if value exist in DB with cursor & if it's null you make an insert otherwise you make an update if the required field is different from what you have already. In any other case do nothing // Some pseudocode

Cursor cursor=_db.query("TABLE", null, TABLEID + "=?", new String[]{id}, null, null, null); if(cursor.getCount()<1) // row Not Exist { cursor.close(); ....assign values SQLiteDatabase _db.insert("TABLE_Name", null, Table_object); download ...ok } cursor.moveToFirst(); if( cursor[field] != "same_value" ) { SQLiteDatabase _db.update("TABLE_Name", null, Table_object); download ...ok } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.