title: Qt multithreaded transactions in SQLite
body: <p>How can one get access to transactions using <a href="https://www.sqlite.org/docs.html" rel="nofollow noreferrer">SQLite</a> through <a href="http://doc.qt.io/qt-5/qsqldatabase.html" rel="nofollow noreferrer">QSqlDatabase</a> ?</p>
  
  <p>I open the database as follows:</p>
  
  <pre><code>m_db = QSqlDatabase::addDatabase("QSQLITE", connection_name);
  m_db.setDatabaseName(db_name);
  m_db.open();
  </code></pre>
  
  <p>I create two such connections - for my Read and Write threads.
  The problem is that I have a large data stream to write, therefore I have to use begin and commit transactions at least in the Write thread. 
  Write thread works well, but I cannot make any select request until Write thread closed its connection.
  Here is how I use transactions:</p>
  
  <pre><code>m_db.transaction(); // BEGIN TRANSACTION
  
  // Many QSqlRequest-s like so:
  QString insert = "INSERT INTO mytable (x, y, z) VALUES (:x, :y, :z);"
  QSqlQuery query(m_db);
  bool ok = query.prepare(insert);
  if (ok)
  {
      query.addBindValue(x);
      query.addBindValue(y);
      query.addBindValue(z);
      //...
      if (query.exec())
      {
          // ok
      }
      else
      {
          // show error
      }
  }
  else
  {
      // show error
  }
  
  m_db.commit(); // COMMIT TRANSACTION
  </code></pre>
  
  <p>This is a common using.
  The problem is these standard operations don't support multithreading using. <strong>How can I get access to normal multithreading transactions?</strong> QSqlDatabase supports threads by default, and I can read/write in both threads without transactions, but transactions are necessary.
  How can I setup all usual multithreading options using QSqlDatabase and SQLite?</p>
  
  <p>I can invoke <code>q.exec("BEGIN IMMEDIATE  TRANSACTION");</code> , but it returns error while using in both threads:  <code>database is locked Unable to fetch row</code>.</p>
  
  <p>Links:<br>
  <a href="https://stackoverflow.com/questions/6369677/database-is-locked-error-in-sqlite3-with-qt">&quot;Database is locked&quot; error in SQLite3 with Qt</a><br>
  <a href="https://stackoverflow.com/questions/3943398/why-does-sqlite-give-a-database-is-locked-for-a-second-query-in-a-transaction">Why does SQLite give a &quot;database is locked&quot; for a second query in a transaction when using Perl&#39;s DBD::SQLite?</a></p>