qt中的数据库可以创建在主函数中吗_在Qt中使用SQLite自定义函数

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.

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(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;

}

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!

解决方案

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: I would probably prefer to leave out the const, as we are modifying the db.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值