SQLite3 是否在 Node.js 中准备了语​​句?

Does SQLite3 have prepared statements in Node.js?

从 npm 文档中,只有可见的准备好的语句用于插入。这些准备好的语句是否适用于 Select、更新和删除?

我试过 select,没有 .each 行回调函数。任何人都能够做到这一点或有资源链接,因为我可以肯定找不到任何资源。

根据 node-sqlite3 API documentation,您可以通过几种不同的方式在 SQL 查询中使用参数:

// Directly in the function arguments.
db.run("UPDATE tbl SET name = ? WHERE id = ?", "bar", 2);

// As an array.
db.run("UPDATE tbl SET name = ? WHERE id = ?", [ "bar", 2 ]);

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

是的,支持预处理语句。

node-sqlite3:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('data.db');

db.serialize(function() {

  var stmt = db.prepare("INSERT INTO users VALUES (?,?)");
  for (var i = 0; i < 10; i++) {
      stmt.run("user " + i, "email " + i);
  }
  stmt.finalize();

  stmt = db.prepare("SELECT * FROM users WHERE id=?");
  stmt.each(userId, function(err, row) {
      console.log(row.name, row.email);
  }, function(err, count) {
      stmt.finalize();
  });

});

better-sqlite3:

var Database = require('better-sqlite3');
var db = new Database('foobar.db', options);

var stmt = db.prepare("INSERT INTO users VALUES (?,?)");
for (var i = 0; i < 10; i++) {
    stmt.run("user " + i, "email " + i);
}

var stmt = db.prepare('SELECT * FROM users WHERE id=?');
var row = stmt.get(userId);
console.log(row.name, row.email);