How to Set Run Values for SQLite Upsert #1219
-
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') |
Beta Was this translation helpful? Give feedback.
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
`); |
Beta Was this translation helpful? Give feedback.
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
id
to the same value: