Receiving error: "Failed to import SQL; message=sqlite3_prepare_v2 failure: near "IF": syntax error" in SQL Statement

Viewed 319

I have a long SQL file to set up a DB for an app. I create multiple tables:

CREATE TABLE
IF NOT EXISTS emotions
(date TEXT, emotions TEXT);

CREATE TABLE
IF NOT EXISTS dates
(date TEXT, textMorning TEXT, textAfternoon TEXT, textEvening TEXT);

CREATE TABLE
IF NOT EXISTS comboEmotions
(emotion TEXT, comboEmotion1 TEXT, comboEmotionColour1 TEXT, etc...)

I then attempt to add the table "comboEmotions" and fill it with data using multiple statements following this format:

IF NOT EXISTS (SELECT * FROM comboEmotions WHERE emotion='joy')
BEGIN
INSERT INTO comboEmotions ('joy', 'none', 'none', 'love', '#C8D23F' ,'guilt', '#93BE4F', 'delight', '#98BD86', 'none', 'none', 'morbidness', '#CAA881', 'pride', '#E5824F', 'optimism', '#F2BA47');
END


IF NOT EXISTS (SELECT * FROM comboEmotions WHERE emotion='trust')
BEGIN
INSERT INTO comboEmotions ('trust', 'love', '#C8D23E', 'none', 'none', 'submission', '#65B744' ,'curiosity', '#6DB77A', 'sentimental', '#86B37C', 'none', 'none', 'dominance', '#B97E45', 'hope', '#C5B135');
END

The line that it appears to break on is the statement:

IF NOT EXISTS (SELECT * FROM comboEmotions WHERE emotion='joy')
BEGIN

Is this statement incorrectly formatted?

1 Answers

Use INSERT... SELECT syntax:

INSERT INTO comboEmotions(emotion, comboEmotion1, comboEmotionColour1, ....) 
SELECT 'joy', 'none', 'none', ...
WHERE NOT EXISTS (SELECT * FROM comboEmotions WHERE emotion='joy');

See the demo.

Related