在Qt中使用SQLite自定义函数 [英] Using SQLite custom functions with Qt

查看:329
本文介绍了在Qt中使用SQLite自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为与Qt一起使用的SQLite数据库创建自定义函数。我找到了有关如何创建该函数的信息,并且该函数似乎可以在x86系统上正常工作。

I'm trying to create a custom function for a SQLite database I'm using with Qt. I found information on how to create the function and it seems to work correctly on a x86 system.

相反,它似乎在ARM设备上出现段错误而失败。这是我写的代码:

Instead, it seems to be failing with a segfault on an ARM device. This is the code I wrote:

static bool createSQLiteFunctions(const QSqlDatabase& db)
{
// Get handle to the driver and check it is both valid and refers to SQLite3.
QVariant v = db.driver()->handle();
if (!v.isValid() || qstrcmp(v.typeName(), "sqlite3*") != 0) {
LOG_WARNING("Cannot get a sqlite3 handle to the driver.");
return false;
}

// Create a handler and attach functions.
sqlite3* handler = *static_cast<sqlite3**>(v.data());
if (!handler) {
LOG_WARNING("Cannot get a sqlite3 handler.");
return false;
}

// Check validity of the state.
if (!db.isValid()) {
LOG_ERROR("Cannot create SQLite custom functions: db object is not valid.");
return false;
}

if (!db.isOpen()) {
LOG_ERROR("Cannot create SQLite custom functions: db object is not open.");
return false;
}

if (sqlite3_create_function(handler, "_deleteFile", 1, SQLITE_ANY, 0, &_sqlite3DeleteFile, 0, 0))
LOG_ERROR("Cannot create SQLite functions: sqlite3_create_function failed.");

return true;
}

db对象被实例化为另一个对象的成员,这称为该对象构造函数中的函数,在该函数中建立了到db的连接(可以同时创建多个实例,但是使用线程安全选项编译sqlite3)。
似乎没有打印错误日志,但是sqlite3_create_function函数给出了段错误。如果我删除对createSQLiteFunctions的调用,则一切正常。
知道为什么结果是段错误吗?
谢谢!

The db object is instantiated as a member of another object, which is calling this function in the constructor, where the connection to the db is established (multiple instances may be created concurrently, but sqlite3 is compiled with thread-safe option). It seems that no error log is printed and, but the sqlite3_create_function function gives a segfault. If I remove the call to createSQLiteFunctions everything works fine. Any idea why the result is a segfault? Thanks!

推荐答案

如果在ARM上出现故障,则可能必须进行编译(您使用的是什么sqlite3?)或回调函数本身(_sqlite3DeleteFile)。它是如何定义的,它的位置/链接在哪里。根据不同的ARM(您使用的是ARM),处理器可能会非常挑剔。
检查您的映射文件,也许是MMU配置,...?

If it's failing on ARM, it may have to do something with compilation (what sqlite3 are you using?) or with the callback function itself (_sqlite3DeleteFile). How is it defined, where is it located / linked. Depending on the ARM (what ARM are you using) the processor can be very picky with alignment. Check your map file, perhaps the MMU-configuration, ...?

BTW:我可能更希望省略const,因为我们正在修改

BTW: I would probably prefer to leave out the const, as we are modifying the db.

static bool createSQLiteFunctions(/*const*/ QSqlDatabase& db)

您可以调试进入sqlite3_create_function代码吗?你能产生痕迹吗?

Can you debug-step into the sqlite3_create_function code? Can you produce a trace?

这篇关于在Qt中使用SQLite自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆