title: "SQLITE_ERROR: cannot start a transaction within a transaction" with BEGIN ... COMMIT transaction control
body: <p>I had been getting the above-mentioned "SQLITE_ERROR: cannot start a transaction within a transaction" error during my database initialization process, but did find that there were some suggestions for best practises: <a href="https://stackoverflow.com/questions/18899828/best-practices-for-using-sqlite3-node-js">here</a> and <a href="https://github.com/mapbox/node-sqlite3/issues/3" rel="nofollow noreferrer">here</a>.</p>
  
  <p>So, I adjusted my code to add the db.run("BEGIN") and db.run("COMMIT") bookends to hopefully fix the issue, but the error still persists.</p>
  
  <p>Here is what my code looks like:</p>
  
  <p>I call the various initialization functions:</p>
  
  <pre><code>intializeMyTable1(...);
  intializeMyTable2(...);
  ...
  intializeMyTableN(...);
  </code></pre>
  
  <p>Each intialize function is roughly the same:</p>
  
  <pre><code>initializeMyTable1: function(...){ 
  ...          
  db.run("BEGIN")           
  db.run("CREATE TABLE if not exists MyTable1 (...)", function (err) {
    if (err !== null) {
      logger.error("Database Error:" + err);
    }
    else {
      for (var i = 0; i &lt; SomeNumber; i++) {
        db.run("INSERT INTO MyTable1 (...) VALUES (...)", function (err) {
          if (err !== null) {
            ...
          }
        });
      }
    }
  });
  db.run("COMMIT");
  ...
  }
  </code></pre>
  
  <p>I've tried db.run("END") instead of db.run("COMMIT") - same error.</p>
  
  <p>What I did find is that if I run only one of the initialization functions at a time, I don't get the error, but if I chain the initialization functions, running them one after the other, I get the error. </p>
  
  <p>Any ideas/suggestions are appreciated!</p>
  
  <p>EDITS: Minor typo fixes</p>