【发布时间】:2019-10-04 14:05:32
【问题描述】:
错误
{ db.transaction(function(tx){ TypeError: db.transaction 不是 函数}
我正在我的 SQLite 数据库中设置题库备份,因此我有 JSON 文件格式的题库。我已经编写了以下代码,但由于我刚开始使用 nodejs 进行编码,因此无法收到错误消息。
var fs = require("fs");
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database('json.sqlite3');
var file = process.env.CLOUD_DIR + "json.sqlite3";
var exists = fs.existsSync(file);
var jsonfile = {
key: "myvalue"
};
fs.readFile('demo.json', function (err, data) {
if (err) {
return console.error(err);
}
console.log("File content: " + data.toString());
// now save to db
if(!exists) {
console.log("Creating DB file.");
fs.openSync(file, "w");
}
db.serialize(function() {
if(!exists) {
db.run("CREATE TABLE Stuff (thing TEXT)");
}
var jsonString = escape(JSON.stringify(data.toString()));
db.transaction(function(tx){
tx.executeSql('INSERT OR REPLACE INTO Stuff(md5, json,
expires) VALUES ("'+hash+'", "'+jsonString+'","'+expireStamp+'")');
},function(tx,error){
console.log(JSON.stringify(tx.message));
console.log(JSON.stringify(error));
},function(){
});
});
});
db.close();
JSON 应该很容易从 SQLite 数据库中更新、编辑和检索
【问题讨论】: