How to Set Run Values for SQLite Upsert #1219
Answered
by
Prinzhorn
cliftonlabrum
asked this question in
Q&A
|
I'm trying to do the SQLite-equivalent of an "upsert" which includes an const insert = db.prepare(`INSERT INTO achievement
(id, name, kind)
VALUES (?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
id = ?, name = ?, kind = ?`)
.run(1, 'Tom', 'Certificate')Do I repeat the //Do I need to do this?
.run(1, 'Tom', 'Certificate', 1, 'Tom', 'Certificate')
//Or this?
.run(1, 'Tom', 'Certificate') |
Answered by
Prinzhorn
Jul 6, 2024
Replies: 1 comment 1 reply
|
Did you try it? The first one gives
because you have six const insert = db.prepare(`
INSERT INTO achievement
( id, name, kind) VALUES
(:id, :name, :kind)
ON CONFLICT(id) DO UPDATE SET
id = :id, name = :name, kind = :kind
`);
insert.run({ id: 1, name: 'Tom', kind: 'Certificate' });They are also more robust if you later change the query, e.g. there is absolutely no reason you are updating const insert = db.prepare(`
INSERT INTO achievement
( id, name, kind) VALUES
(:id, :name, :kind)
ON CONFLICT(id) DO UPDATE SET
name = :name, kind = :kind
`); |
1 reply
Answer selected by
cliftonlabrum
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you try it? The first one gives
because you have six
?but only provided three values. Numbered placeholders are not supported yet (#725), so I recommend using named parameters.They are also more robust if you later change the query, e.g. there is absolutely no reason you are updating
idto the same value: