首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >希望将json数据从文件存储到sqlite数据库,但得到错误

希望将json数据从文件存储到sqlite数据库,但得到错误
EN

Stack Overflow用户
提问于 2019-05-17 09:01:54
回答 1查看 401关注 0票数 0

误差

{ db.transaction (函数(Tx)){ TypeError: db.transaction不是函数}

我正在我的SQLite数据库中设置一个问题库备份,所以我有JSON文件形式的问题库。我编写了以下代码,但由于我刚刚开始使用nodejs编写代码,所以无法得到错误。

代码语言:javascript
运行
复制
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();

应该很容易地从SQLite数据库中更新、编辑和检索JSON

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 12:24:39

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

查看API文档以获得数据库对象上可用方法的列表。

我发现本教程在学习过程中非常有用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56183044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档