我正在使用浏览器和JavaScript从磁盘打开sqlite3数据库文件,更新并插入数据,然后将其再次保存到同一文件中。 为此,我正在使用sql.js和browserify。
Browserify命令:
browserify main.js -o bundle.js
Index.html:
<!doctype html>
<html>
<head>
</head>
<body>
<div class="container" id="container">
<div>
<input type="file" id="dbfile"></input>
</div>
<div>
<button id="savedb">Guardar</button>
</div>
</div>
<script src="bundle.js"></script>
</body>
Main.js:
const dbFileElm = document.getElementById('dbfile');
const savedbElm = document.getElementById('savedb');
let sql = require('sql.js');
let db;
// Read the database from the disk
dbFileElm.addEventListener('change', () => {
let f = dbFileElm.files[0];
let r = new FileReader();
r.onload = function() {
let Uints = new Uint8Array(r.result);
db = new sql.Database(Uints);
}
r.readAsArrayBuffer(f);
})
// Write a database to the disk
savedbElm.addEventListener('click', () => {
var data = db.export();
//how to write?
})
如何将数据库写入磁盘文件?