title: Adding a custom sqlite function to a Qt application
body: <p>I am trying to add a custom sqlite3 <code>regexp</code> function into my Qt application (as recommended by <a href="https://stackoverflow.com/a/8338515/1447002">this answer</a>). But as soon as I call the <code>sqlite3_create_function</code> function, I get the message <code>The program has unexpectedly finished.</code> When I debug, it terminates in a segmentation fault in <code>sqlite3_mutex_enter</code>. There is a MWE below, with apologies for the absolute file paths.</p>
  
  <p>The <code>regexp</code> implementation in my code is from <a href="http://www.qtcentre.org/threads/48969-How-to-create-a-custom-function-in-a-QSqlDatabase-regex-in-sqlite-and-pyqt" rel="nofollow noreferrer">this site</a>; it also fails with the <code>msign</code> function <a href="http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html" rel="nofollow noreferrer">here</a>. The various checks of <code>driver()-&gt;handle()</code> are straight from the Qt docs.</p>
  
  <p>Incidentally, I used <code>select sqlite_version();</code> to determine that Qt 5.5 uses sqlite version 3.8.8.2. I found <a href="https://github.com/qtproject/qtbase/tree/611bcc3cd337fe77ea5d3b898d897582da6f7ee2/src/3rdparty/sqlite" rel="nofollow noreferrer">that version</a> by looking through old commits in the Qt GitHub repository.</p>
  
  <p>MWE.pro</p>
  
  <pre><code>QT       += core gui
  TARGET = MWE
  TEMPLATE = app
  QT += sql
  SOURCES += main.cpp \
      D:\Qt\Qt5.5.0\5.5\Src\3rdparty\sqlite\sqlite3.c
  
  HEADERS  += D:\Qt\Qt5.5.0\5.5\Src\3rdparty\sqlite\sqlite3.h
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;QtSql&gt;
  
  #include "D:/Qt/Qt5.5.0/5.5/Src/3rdparty/sqlite/sqlite3.h"
  
  void qtregexp(sqlite3_context* ctx, int argc, sqlite3_value** argv)
  {
      QRegExp regex;
      QString str1((const char*)sqlite3_value_text(argv[0]));
      QString str2((const char*)sqlite3_value_text(argv[1]));
  
      regex.setPattern(str1);
      regex.setCaseSensitivity(Qt::CaseInsensitive);
  
      bool b = str2.contains(regex);
  
      if (b)
      {
          sqlite3_result_int(ctx, 1);
      }
      else
      {
          sqlite3_result_int(ctx, 0);
      }
  }
  
  int main(int argc, char *argv[])
  {
      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("my.db");
      db.open();
  
      QVariant v = db.driver()-&gt;handle();
      if (v.isValid() &amp;&amp; qstrcmp(v.typeName(), "sqlite3*")==0) {
          sqlite3 *db_handle = *static_cast&lt;sqlite3 **&gt;(v.data());
          if (db_handle != 0) { // check that it is not NULL
              // This shows that the database handle is generally valid:
              qDebug() &lt;&lt; sqlite3_db_filename(db_handle, "main");
              sqlite3_create_function(db_handle, "regexp", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, &amp;qtregexp, NULL, NULL);
              qDebug() &lt;&lt; "This won't be reached."
  
              QSqlQuery query;
              query.prepare("select regexp('p$','tap');");
              query.exec();
              query.next();
              qDebug() &lt;&lt; query.value(0).toString();
          }
      }
      db.close();
  }
  </code></pre>