Eric Grange
2018-11-30 09:14:48 UTC
Hi,
I am running into a little trouble when trying to use and "upsert" from a
select clause.
Starting from the "vocabulary" exemple at
https://sqlite.org/lang_UPSERT.html this works
INSERT INTO vocabulary(word, count)
SELECT 'jovial', 1
ON CONFLICT(word) DO UPDATE SET count=count+1
but as soon as the "SELECT" has a from clause it does not seem to be
working (near "DO": syntax error)
INSERT INTO vocabulary(word, count)
SELECT * FROM (SELECT 'jovial', 1)
ON CONFLICT(word) DO UPDATE SET count=count+1
(without the ON CONFLICT clause the above is accepted)
I have tried to place the SELECT between parenthesis, but SQLite then
complains of an error on the opening parenthesis.
Any workarounds ?
My actual usage case would actually use a json_each to provide the values
(in case that throws an extra spanner...), like in
INSERT INTO vocabulary (word, count)
SELECT atom, 1 from json_each('["alpha","beta"]')
ON CONFLICT(word) DO UPDATE SET count=count+1
Eric
I am running into a little trouble when trying to use and "upsert" from a
select clause.
Starting from the "vocabulary" exemple at
https://sqlite.org/lang_UPSERT.html this works
INSERT INTO vocabulary(word, count)
SELECT 'jovial', 1
ON CONFLICT(word) DO UPDATE SET count=count+1
but as soon as the "SELECT" has a from clause it does not seem to be
working (near "DO": syntax error)
INSERT INTO vocabulary(word, count)
SELECT * FROM (SELECT 'jovial', 1)
ON CONFLICT(word) DO UPDATE SET count=count+1
(without the ON CONFLICT clause the above is accepted)
I have tried to place the SELECT between parenthesis, but SQLite then
complains of an error on the opening parenthesis.
Any workarounds ?
My actual usage case would actually use a json_each to provide the values
(in case that throws an extra spanner...), like in
INSERT INTO vocabulary (word, count)
SELECT atom, 1 from json_each('["alpha","beta"]')
ON CONFLICT(word) DO UPDATE SET count=count+1
Eric