Add a custom SQLITE function to the QT application

I am trying to add a custom sqlite3 regexp function to my Qt application (as suggested in this answer). But when I call the sqlite3_create_function function, I get the message that the program Finished unexpectedly. When I debugged, it ended with a segmentation fault in sqlite3_mutex_enter. There is a MWE below that apologizes for the absolute file path.

The regexp implementation in my code is from This site started; it also fails with the msign function here. The various checks on driver() -> handle() come directly from the Qt documentation.

By the way, I use select sqlite_version(); OK Qt 5.5 uses sqlite version 3.8.8.2. I found that version by looking at the old commits in the Qt GitHub repository.

MWE.pro

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

main .cpp

#include 

#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
{< br /> 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()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0) {
sqlite3 *db_handle = *static_cast< sqlite3 **>(v.data());
if (db_handle != 0) {// check that it is not NULL
// This shows that the database handle is generally valid:
qDebug() << sqlite3_db_filename(db_handle, "main");
sqlite3_create_function(db_handle, "regexp", 2, SQLITE_UTF8 | SQLITE_DETERMINIS TIC, NULL, &qtregexp, NULL, NULL);
qDebug() << "This won't be reached."

QSqlQuery query;
query.prepare("select regexp('p$','tap');");
query.exec();
query.next();
qDebug() << query.value(0) .toString();
}
}
db.close();
}

After obtaining the database handle from Qt, according to this forum post, you need to call sqlite3_initialize().

...< br /> sqlite3 *db_handle = *static_cast(v.data());
if (db_handle != 0) {// check that it is not NULL
sqlite3_initialize();
...

This solves the problem.

I am trying to add a custom sqlite3 regexp function to my Qt application In the program (as suggested in this answer). But when I call the sqlite3_create_function function, I get the message that the program completed unexpectedly. When I debug, it terminates with a segmentation fault in sqlite3_mutex_enter. There is a MWE below, right Apologies for the absolute file path.

The regexp implementation in my code started from this site; it also failed with the msign function here. Various checks on driver() -> handle() Directly from the Qt documentation.

< p>By the way, I use select sqlite_version(); to determine that Qt 5.5 uses sqlite version 3.8.8.2. I found that version by looking at the old commits in the Qt GitHub repository.

MWE.pro< /p>

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

main.cpp

#include 

#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]));
< br /> regex.setPattern(str1);
regex.setCaseSensitivity(Qt::CaseInsensitive);

bool b = str2.contains(regex);

if (b)
{
sqlite3_result_int(ctx, 1);
}
else
{
sqlite3_resu lt_int(ctx, 0);
}
}

int main(int argc, char *argv[])
{
QSqlDatabase db = QSqlDatabase ::addDatabase("QSQLITE");
db.setDatabaseName("my.db");
db.open();

QVariant v = db.driver() ->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0) {
sqlite3 *db_handle = *static_cast(v.data());
if (db_handle != 0) {// check that it is not NULL
// This shows that the database handle is generally valid:
qDebug () << sqlite3_db_filename(db_handle, "main");
sqlite3_create_function(db_handle, "regexp", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, &qtregexp, NULL, NULL);
qDebug() << " This won't be reached."

QSqlQuery query;
query.prepare("select regexp('p$','tap');");
query. exec();
query.next();
qDebug() << query.value(0).t oString();
}
}
db.close();
}

From Qt, After obtaining the database handle according to this forum post, you need to call sqlite3_initialize().

...
sqlite3 *db_handle = *static_cast(v.data());
if (db_handle != 0) {// check that it is not NULL
sqlite3_initialize();
...

This solved the problem.

Leave a Comment

Your email address will not be published.