News for hackers. Stuff that matters.
Re:SQLite: Small, Fast, Reliable – Choose any three by kennywinker on Tuesday July 15, 2014 @02:51AM (#8035134)
Just today I encountered an "insert or update" situation and handled it like this:

    INSERT OR IGNORE INTO table_name (item_key, item_count) VALUES (?, 0);
    UPDATE table_name SET item_count = item_count+1 WHERE item_key = ?;
I'm not familiar with "upsert" so maybe there is a difference, but this worked well for my use case (which was "increment or insert then increment"). You could even reverse the operations (by adding OR IGNORE to the UPDATE statement), if you wanted to not do two operations if the pk didn't exist yet.

There are no comments on this comment yet.