2

I would like to implement this:

If exist update Else insert 

It seems that I am touching the database two times. Is there any shorter way to implement this in sqlite3?

2
  • What about: update; if no rows were updated, then insert. Works best in an update-mostly scenario. Don't know how to implement in sqlite though. Commented Nov 19, 2012 at 15:51
  • @HansKesting - That's still two touches of the data for an insert, which the op is still trying to avoid. I don't know SQLite3 so I don't know if it support MERGE or any other UPSERT type of syntax. Commented Nov 19, 2012 at 15:57

1 Answer 1

2

If you have a unique constraint on the key field, you could use the INSERT OR REPLACE command which automatically deletes the old record if the new one would create a duplicate.

However, this does not give you a performance advantage: There must always be some check for the record; whether you are doing an explicit SELECT or are using SQLite's built-in duplicate detection does not make much of a difference.

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

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.