问题描述
我正在尝试将自定义 sqlite3 regexp
函数添加到我的 Qt 应用程序中(如 this answer 推荐的那样)).但是一旦我调用 sqlite3_create_function
函数,我就会收到消息 The program has unexpectedly completed.
当我调试时,它在 sqlite3_mutex_enter.下面有一个 MWE,对绝对文件路径表示歉意.
I am trying to add a custom sqlite3 regexp
function into my Qt application (as recommended by this answer). But as soon as I call the sqlite3_create_function
function, I get the message The program has unexpectedly finished.
When I debug, it terminates in a segmentation fault in sqlite3_mutex_enter
. There is a MWE below, with apologies for the absolute file paths.
我代码中的 regexp
实现来自 本站;它也因 msign
函数此处而失败.driver()->handle()
的各种检查直接来自 Qt 文档.
The regexp
implementation in my code is from this site; it also fails with the msign
function here. The various checks of driver()->handle()
are straight from the Qt docs.
顺便说一句,我使用select sqlite_version();
来确定Qt 5.5使用的是sqlite 3.8.8.2版本.我通过查看 Qt 中的旧提交找到了那个版本GitHub 存储库.
Incidentally, I used select sqlite_version();
to determine that Qt 5.5 uses sqlite version 3.8.8.2. I found that version by looking through 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 <QtSql>
#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()->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_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).toString();
}
}
db.close();
}
推荐答案
从Qt获取数据库句柄后需要调用sqlite3_initialize()
,根据此论坛帖子.
You need to call sqlite3_initialize()
after you get the database handle from Qt, according to this forum post.
...
sqlite3 *db_handle = *static_cast<sqlite3 **>(v.data());
if (db_handle != 0) { // check that it is not NULL
sqlite3_initialize();
...
这解决了问题.
这篇关于向 Qt 应用程序添加自定义 sqlite 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!