When I run the code with this query, sometimes I see all the tables listed, sometimes only one, and I always get this error:
Query error: error: SQLITE_MISUSE: unknown error
I read that SQLITE_MISUSE occurs when the SQLITE API is used slowly. Could you help me because I cannot find something wrong in this code.
EDIT. I made changes to the code to get rid of the race problem.
The SQLITE_MISUSE violation message still occurs, but the problem with the disappearing tables has disappeared. In my requests there was a race.
Here is the code.
var sqlite3 = require("node-sqlite3"); var fs = require('fs'); var query_count; var init = function (response) { var db = new sqlite3.Database("test.db", function() { fs.readFile('./assets/sql/initDB.sql', function(err,data){ if(err) { console.error("Could not open file: %s", err); return; } var query = data.toString('utf8'); queries = query.split(";"); db.serialize(function() { query_count = queries.length; for(var i=0; i<queries.length; i++) { queries[i] = queries[i].replace("\r\n",""); db.run(queries[i], function(error) { if(error) { console.log("Query Error: "+error); } query_count--; if( query_count <= 0 ) { db.close(); listAllTables(response); } }); } }); }); }); }; function listAllTables(response) { var db = new sqlite3.Database("./assets/sql/test.db", function () { db.all("SELECT name FROM sqlite_master WHERE type = 'table'", function (error, records) { for(var record in records) { response.write(record+": "+records[record]+"\n"); for(var prop in records[record]) { response.write("\t"+prop+": "+records[record][prop]+"\n"); } } response.end(); db.close(); }); }); } exports.load_customers = function(response) { init(response); };
The initDB.sql request file is as follows:
CREATE TABLE IF NOT EXISTS TemporaryAuthTokens ( authToken TEXT PRIMARY KEY NOT NULL UNIQUE, expireDate NUMERIC NOT NULL); CREATE TABLE IF NOT EXISTS User ( id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , login TEXT NOT NULL , pass TEXT NOT NULL , creationDate NUMERIC NOT NULL , authToken TEXT NULL REFERENCES TemporaryAuthTokens(authToken) );
source share