我正在尝试将自定义sqlite3 regexp
函数添加到我的Qt应用程序中(按照此答案的建议).但是一旦我调用该sqlite3_create_function
函数,我就会收到消息.The program has unexpectedly finished.
当我调试时,它会终止分段错误sqlite3_mutex_enter
.下面有一个MWE,对绝对文件路径道歉.
regexp
我的代码中的实现来自这个站点 ; 它也失败了这里的msign
功能.各种检查直接来自Qt文档.driver()->handle()
顺便说一下,我曾经select sqlite_version();
确定Qt 5.5使用的是sqlite版本3.8.8.2.我通过查看Qt GitHub存储库中的旧提交找到了该版本.
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 { 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 (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(); }
adam.baker.. 6
根据这篇论坛帖子sqlite3_initialize()
,你需要在从Qt获得数据库句柄后调用.
... sqlite3 *db_handle = *static_cast(v.data()); if (db_handle != 0) { // check that it is not NULL sqlite3_initialize(); ...
这解决了这个问题.
根据这篇论坛帖子sqlite3_initialize()
,你需要在从Qt获得数据库句柄后调用.
... sqlite3 *db_handle = *static_cast(v.data()); if (db_handle != 0) { // check that it is not NULL sqlite3_initialize(); ...
这解决了这个问题.