INSERT or REPLACE if exist in SQLite

You create a table class:

CREATE TABLE class (
	id INTEGER PRIMARY KEY AUTOINCREMENT,
	full_name TEXT UNIQUE,
	subject INTEGER,
	score INTEGER
);

Then you want to add a new record with unique full_name, if the record already existed, then replace.

INSERT OR REPLACE INTO class (id, full_name, subject, score) VALUES (
	(SELECT ID FROM class WHERE full_name = 'search_name'),
	'search_name',
	'subject'
	'score');

Need a good GUI tool for SQLite? Check out TablePlus. It’s native, beautiful, and available for free.

Download TablePlus for Mac.

Not on Mac? Download TablePlus for Windows.

On Linux? Download TablePlus for Linux

Need a quick edit on the go? Download TablePlus for iOS.

TablePlus GUI Tool SQLite