Home > Back-end >  SQLite3 prepare statements using named parameters in Node.js
SQLite3 prepare statements using named parameters in Node.js

Time:09-26

I know I can do this;

// As an object with named parameters.
db.run("UPDATE tbl SET name = $name WHERE id = $id", {
  $id: 2,
  $name: "bar"
});

What about using the prepare statement like this?

var stmt = db.prepare("INSERT INTO tbl (column_one, column_two ) values ($column_one, $column_two)");

stmt.run("first value of column 1", "first value of column 1");
stmt.run("second value of column 1", "second value of column 1");
stmt.run("third value of column 1", "third value of column 1");

stmt.finalize();

How can I bind the value into a specific parameter?

CodePudding user response:

Alternatively, you can do the following

var query = "INSERT INTO tbl (column_one, column_two ) values ($column_one, $column_two)";

db.run(query, {$column_one: "your value", $column_two: "your value"});
db.run(query, {$column_one: "your value", $column_two: "your value"});
db.run(query, {$column_one: "your value", $column_two: "your value"});

or with prepare statement

var stmt = db.prepare("INSERT INTO tbl (column_one, column_two ) values (?, ?)");

stmt.run("first value", "second value");
stmt.run("first value", "second value");
stmt.run("first value", "second value");

stmt.finalize();
  • Related