So I am trying the following:
JavaScript
x
16
16
1
var sqlite3 = require('sqlite3').verbose();
2
var db = new sqlite3.Database('MyDB.db');
3
4
console.log("Initializing...")
5
var MyGUID = "d49dfb07-7ac9-42e7-a355-2707209baea5";
6
db.serialize(function() {
7
db.all("SELECT Name, Status FROM MyTable WHERE MyGUID = ? ", [MyGUID], function(err, rows) {
8
console.log(rows);
9
db.run("UPDATE MyTable SET readOnly = 0 WHERE MyGUID = 'd49dfb07-7ac9-42e7-a355-2707209baea5'", "", function(err) {
10
console.log(err);
11
});
12
});
13
});
14
15
db.close();
16
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.
JavaScript
1
8
1
db.serialize(function() {
2
db.all("SELECT Name, Status FROM MyTable WHERE MyGUID = ? ", [MyGUID], function(err, rows) {
3
console.log(rows)
4
});
5
db.run("UPDATE MyTable SET readOnly = 0 WHERE MyGUID = 'd49dfb07-7ac9-42e7-a355-2707209baea5'", "", function(err) {
6
console.log(err);
7
});
8
});