title: SQLite3 do not always work properly (cannot prepare or execute queries)
body: <p>I am using an MacMini for development in swift and I am testing on an iPhone 6S.</p>
  
  <p>I tried to install Sqlite-wrappers (SQLite.swift, fmdb) but they both crashed my app on startup. So I use the pure sqlite3-API.</p>
  
  <p>At first I created a pointer to the DB and made it available as a static variable.</p>
  
  <pre><code>static var db: OpaquePointer? = nil
  .....
  sqlite3_open(fileURL.path, &amp;db)
  </code></pre>
  
  <p>But I got sometimes a Bad_Access crash. Searched the net. Found "do not use 1 pointer in different threads".</p>
  
  <p>So now I create a pointer at the moment I need it in the different threads with open-statement, do always a sqlite3_reset, a finalize and then I close it.</p>
  
  <pre><code>static func getDBPointer() -&gt; OpaquePointer? {
      var db: OpaquePointer? = nil
      if sqlite3_open(dbFile, &amp;db) != SQLITE_OK {
          print("error opening database")
      } else {
          print("******************* DB opened")
      }
      return db
  }
  </code></pre>
  
  <p>Worked fine for a few days, no problems, no Bad_Access.</p>
  
  <p>But now all the DB behaves very strange.
  It worked fine for a minute and then I cant insert or update rows and the next statement works fine again.
  "delete * from table" is no problem most of the time, but now and then it fails. All sqlite3_prepare_v2()-Statements worked fine and I can see no syntax problem.
  On the next try (same statements, all wellformed with proper data) I get nearly always "Failed to prepare ..."</p>
  
  <p>Here a short example of code:</p>
  
  <pre><code>let db = getDBPointer()
  
  let insertStatementString = "INSERT INTO vorstellung_info (vorstellung_nr,datum,vorstellung,haus,autor) VALUES (\(vorst_num),\(vorst_datum),'\(vorstellung_name)','\(vorst_haus)','\(vorst_autor)');"
  
  print("Insert vorstellung string: \(insertStatementString)")
  
  var insertStatement: OpaquePointer? = nil
  
  if sqlite3_prepare_v2(db, insertStatementString, -1, &amp;insertStatement, nil) == SQLITE_OK {
      if sqlite3_step(insertStatement) == SQLITE_DONE {
          print("Done well.")
      } else {
          print("Could not insert row in vorstellung_info: \(insertStatementString)")
      }
  } else {
      print("INSERT statement to vorstellung_info could not be prepared.")
  }
  sqlite3_finalize(insertStatement)
  sqlite3_close_v2(db)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Insert vorstellung string: INSERT INTO vorstellung_info (vorstellung_nr,datum,vorstellung,haus,autor) VALUES (45127,1481382000000,'TEST-WantToSee','Thats the place','Interesting author');
  Could not insert row in vorstellung_info: INSERT INTO vorstellung_info (vorstellung_nr,datum,vorstellung,haus,autor) VALUES (45127,1481382000000,'TEST-WantToSee','Thats the place','Interesting author');
  </code></pre>
  
  <p>And as I said before: sometimes even the prepare statement fails.</p>
  
  <p>The create table statement for this example:</p>
  
  <pre><code>let createVorstellungInfo = "CREATE TABLE IF NOT EXISTS vorstellung_info(" +
              "vorstellung_nr INT," +
              "datum INT," +
              "vorstellung TEXT," +
              "haus TEXT," +
          "autor TEXT);"
  </code></pre>
  
  <p>The program has a lot of "conversation" with a server via session, so most of the non-select-statements are asynch in a session.task or static func that are called by such task.</p>
  
  <p>Edit:</p>
  
  <p>added "String(cString: sqlite3_errmsg(db))" to any possible location</p>
  
  <p>"cannot prepare":</p>
  
  <pre><code>SELECT statement could not be prepared: SELECT * from rechnung_vorst where rechnung_nr = 9868681 AND vorstellung_nr = 45138;
  ------------------------- 
  database is locked
  </code></pre>
  
  <p>"Cant load"</p>
  
  <pre><code>Veranstalter could not get loaded: SELECT veranstalter_nname, veranstalter_vname, veranstalter_uid, veranstalter_ort, veranstalter_plz, veranstalter_str FROM veranstalter WHERE veranstalter_nr = 2062491;
  ------------------------- 
  unknown error
  </code></pre>
  
  <p>These are only 2 examples of dozends of messages.
  By the way: took me 4 - 5 attempts to get these messages (all within one attempt). First it worked fine, then these errors and then it worked again ...</p>