Skip to content
Advertisement

nodejs sqlite 3 “SQLITE_MISUSE: Database handle is closed” db.run within db.all

So I am trying the following:

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('MyDB.db');

console.log("Initializing...")
var MyGUID = "d49dfb07-7ac9-42e7-a355-2707209baea5";
db.serialize(function() {
    db.all("SELECT Name, Status FROM MyTable WHERE MyGUID = ? ", [MyGUID], function(err, rows) {
        console.log(rows);
        db.run("UPDATE MyTable SET readOnly = 0 WHERE MyGUID = 'd49dfb07-7ac9-42e7-a355-2707209baea5'", "", function(err) {
            console.log(err);
        });
    }); 
});

db.close();

This results in “SQLITE_MISUSE: Database handle is closed” Apparently I cannot run the UPDATE query within the db.all callback. But why is that so?

Advertisement

Answer

You should put the second query as an argument to db.serialize(). Then it will wait for it to complete before returning and allowing db.close() to run.

db.serialize(function() {
  db.all("SELECT Name, Status FROM MyTable WHERE MyGUID = ? ", [MyGUID], function(err, rows) {
    console.log(rows)
  });
  db.run("UPDATE MyTable SET readOnly = 0 WHERE MyGUID = 'd49dfb07-7ac9-42e7-a355-2707209baea5'", "", function(err) {
    console.log(err);
  });
});
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement