I need a way to run some sql commands ONLY IF a record doesn't already exist in my db.
I have the following .sql file so far:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
IF NOT EXISTS (SELECT * FROM widgets WHERE name="123")
BEGIN
ALTER TABLE table2 ADD COLUMN mynewcolumn VARCHAR(255);
INSERT INTO table2 ("abc", "def")
END
COMMIT;
But this fails with a syntax error.
I know that IF does exist in sqlite but it seems it's only a part of the CREATE statement.
Declare variable in sqlite and use it
I'm wondering if you can suggest a different way I can accomplish the task above?