扣丁书屋

在JavaScript中使用sqlite3

特性

  • Straightforward query and parameter binding interface
  • Full Buffer/Blob support
  • Extensive debugging support
  • Query serialization API
  • Extension support
  • Big test suite
  • Written in modern C++ and tested for memory leaks
  • Bundles Sqlite3 3.26.0 as a fallback if the installing system doesn't include SQLite

安装

npm install sqlite3

使用 node-pre-gyp 下载安装预编译好的二进制模块,否则,使用node-gyp来编译扩展。

使用

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

db.serialize(function() {
  db.run("CREATE TABLE lorem (info TEXT)");

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

  db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
      console.log(row.id + ": " + row.info);
  });
});

db.close();

具体文档参考:https://github.com/mapbox/node-sqlite3/wiki