Using SQLite custom functions with Qt



  • Hi! 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<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;
    }@

    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 estabilished:

    @ ...
    // Create the connection to the database. The connection has to be different
    // for each thread requiring one.
    db = QSqlDatabase::addDatabase("QSQLITE", connectionName);

    // Setup the database to avoid lockings.
    db.setConnectOptions("QSQLITE_BUSY_TIMEOUT=10000");

    db.setDatabaseName(MEDIASTORE_DATABASE_FILENAME);
    if (!db.open()) {
    LOG_ERROR("Failed to open the database.");
    // TODO: What to do in case of errors??
    }

    // Create custom defined functions.
    if (!createSQLiteFunctions(db)) {
    LOG_ERROR("Failed to create custom functions.");
    // TODO: What to do in case of errors??
    }
    ...@

    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.

    I suspect this may be related to http://bugreports.qt.nokia.com/browse/QTBUG-16586. Any idea how I can solve this?
    Thanks!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.