SQLite INSERT – ON DUPLICATE KEY UPDATE (UPSERT)

INSERT OR IGNORE INTO visits VALUES ($ip, 0);
UPDATE visits SET hits = hits + 1 WHERE ip LIKE $ip;

This requires the “ip” column to have a UNIQUE (or PRIMARY KEY) constraint.


EDIT: Another great solution: https://stackoverflow.com/a/4330694/89771.

Leave a Comment