【问题标题】:Want to store json data from file to sqlite database but getting error想要将json数据从文件存储到sqlite数据库但出现错误
【发布时间】: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 数据库中更新、编辑和检索

【问题讨论】:

    标签: node.js json sqlite


    【解决方案1】:

    错误消息db.transaction(function(tx){ TypeError: db.transaction is not a function 清晰明确。

    查看API doc 以获取数据库对象的可用方法列表。

    我发现this tutorial 在学习过程中非常有帮助。

    【讨论】:

      猜你喜欢
      • 2015-02-23
      • 2017-12-20
      • 2016-03-05
      • 2014-01-23
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      相关资源
      最近更新 更多