title: Sqlite SQLITE_MISUSE error in node.js script
body: <p>When I launch code with this query, sometimes I can see all the tables listed, sometimes only one and I get always this error:</p>
  
  <p>Query Error: Error: SQLITE_MISUSE: unknown error</p>
  
  <p>I've read that SQLITE_MISUSE occurs when SQLITE API is used unproperly. Could you help me, because I can't find whats wrong in this code.</p>
  
  <p>EDIT. I've made changes to the code to get rid of the race issue.</p>
  
  <p>The message with SQLITE_MISUSE wrror still occurs, however the vanishing tables issue is gone. Race in my queries was the case.</p>
  
  <p>Here is the code.</p>
  
  <pre><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&lt;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 &lt;= 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);
  };
  </code></pre>
  
  <p>The query file initDB.sql is like this:</p>
  
  <pre><code>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)
    );
  </code></pre>