Skip to content

How to Set Run Values for SQLite Upsert #1219

Answered by Prinzhorn
cliftonlabrum asked this question in Q&A
Discussion options

You must be logged in to vote

Did you try it? The first one gives

RangeError: Too few parameter values were provided

because you have six ? but only provided three values. Numbered placeholders are not supported yet (#725), so I recommend using named parameters.

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 id to the same value:

const insert = db.prepare(`
  INSERT INTO achievement
  ( id,  name,  kind) VALUES
  (:id, :n…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cliftonlabrum
Comment options

Answer selected by cliftonlabrum
# for free to join this conversation on GitHub. Already have an account? # to comment
Category
Q&A
Labels
None yet
2 participants