None of the other questions which address this error seems to work for me. My query is:
IF NOT EXISTS (SELECT 1 FROM Configuration WHERE key = 'CookieCount')
BEGIN
INSERT INTO Configuration (key, value)
VALUES ('CookieCount', '0')
END
and my error message is: "could not prepare statement (1 near "IF": syntax error)"
I have this working in Postgres: http://sqlfiddle.com/#!15/b14ef/2/0 But this does not work in SQLite and I need it to work with both.
Try to use something like this instead IF statement :
INSERT INTO Configuration (key, value)
SELECT 'CookieCount', '0'
WHERE NOT EXISTS(SELECT 1 FROM Configuration WHERE key = 'CookieCount');
SQLite doesn't have an IF operator like that, at all. Nearest thing is the CASE... http://www.sqlite.org/lang_expr.html#case, but that doesn't really apply in your situation.
The answer in "Insert if not exists" statement in SQLite applies here, as an insert-if-not-exists statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With