Error
{ db.transaction(function(tx){ TypeError: db.transaction is not a function }
I am setting up a question bank backup in my SQLite database so I have question bank in JSON file form. I have written the following code but couldn't get the error as I just started coding with 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 should be easily updated, edited, and retrieved from the SQLite Database