laksh
laksh

Reputation: 2329

if exist update or else insert in sqlite3

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?

Upvotes: 2

Views: 182

Answers (1)

CL.
CL.

Reputation: 180020

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.

Upvotes: 2

Related Questions