https://github.com/sql-js/sql.js
<meta charset="utf8" />
<html>
<script src='dist/sql-wasm.js'></script>
<script>
const sqlPromise = initSqlJs({
locateFile: file => `/dist/${file}`
}).then(function (SQL) {
const xhr = new XMLHttpRequest();
xhr.open('GET', '/data.db', true);
xhr.responseType = 'arraybuffer';
xhr.onload = e => {
const uInt8Array = new Uint8Array(xhr.response);
const db = new SQL.Database(uInt8Array);
const contents = db.exec("SELECT * FROM panda");
console.log("🚀 ~ contents", contents)
};
xhr.send();
})
</script>
<body>
Output is in Javascript console
</body>
</html>
静态前端如若修改保存需要提交给后端程序操作。
node.js保存方法:
//将数据库导出到包含 SQLite 数据库文件的 Uint8Array
var data = db.export();
// 改用new Buffer.alloc()、Buffer.allocUnsafe()或Buffer.from()构造方法
var buffer = Buffer.from(data, 'binary');
// 被创建数据库名称
var filename = "d.sqlite";
fs.writeFileSync(filename, buffer);
Comments | NOTHING