Sqlite的js编辑库-sql.js

Sqlite的js编辑库-sql.js

2017-04-11 / 0 评论 / 132 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年10月27日,已超过910天没有更新,若内容或图片失效,请留言反馈。

npm安装

npm install sql.js

npm地址

https://www.npmjs.com/package/sql.js

github&文档地址

Github
Document

示例

const path = require('path')
const fs = require('fs');
const sql = require('sql.js');
function getPath() {
  let pathArr = Array.prototype.slice.call(arguments);
  pathArr.unshift(__dirname);
  return path.join(pathArr.join('/'));
}
/**
SQL
**/
function sql(path) {
  this.path = path;
  this.db;
}
sql.prototype = {
  init: function() {
    let filebuffer = fs.readFileSync(this.path);
    let database = new sqljs.Database(filebuffer);
    this.db = database;
  },
  find: function(_sql, _params) {
    let stmt = this.db.prepare(_sql),
      res = [];
    stmt.bind(_params);
    while (stmt.step()) {
      res.push(stmt.getAsObject());
    }
    stmt.free();
    return res;
  },
  findOne: function(_sql, _params) {
    return this.find(_sql, _params)[0];
  },
  run: function(_sql, _params) {
    return this.db.run(_sql, _params);;
  }
};
(function() {
  const SQL = new sql(getPath('data', 'app.sqlite'));
  SQL.init();
  let res = SQL.find("select * from user");
  console.log(res);
})();

0

评论 (0)

取消