I know close to nothing about SQL, but sqlite3 does have a version of UPSERT
called INSERT OR REPLACE
(see this and this SO questions. That may be faster than the IF...UPDATE...ELSE...INSERT that you use now. Something to consider, if I understood it properly :)
Thanks!