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?
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.
update; if no rows were updated, theninsert. Works best in an update-mostly scenario. Don't know how to implement in sqlite though.