Quote Originally Posted by dh7892 View Post
Sorry to dredge up an old post but I have recently had to add my own custom functions to Qt's SQLite drivers and had many of the same problems. Now that I have resolved them and got my code working, I thought I'd add to this post because I found it when searching and it didn't really help me solve the problem so I thought I'd include my solution(s) so anyone else arriving here might save some effort.

There are two ways to go about getting the custom functions to work:

get the sqlite3* pointer from the db as described above. (sqlite3* handler = *static_cast<sqlite3**>(v.data()).

However, if you do it this way, there are some things you need to know:

- You must compile your code to link against sqlite3 (which you will have to install separately from Qt as it doesn't contain enough sqlite so that you could link against it). I just used the unified sqlite header and source files and added them into my project directly rather than linking to a library.
- It seems that you need to be very careful to ensure that the version of sqlite that you link to is exactly the same as the version that Qt used when it was built. Look at the sources for Qt if you need to check.
- When I compiled my code, I had to add the -DSQLITE_THREADSAFE=0 (this was for Qt 4.8) otherwise I got a seg fault in create_function.

The disadvantage of this approach is that you will need to change your source to match whatever version of Qt you are using.

The approach I ended up using was to create my own version of the SQLite database driver. I just copied the relevant code from the Qt source and modified it to use a my local copy of SQLite instead of Qt's version. I also added my custom functions in when the database opens in the driver.

I hope some of this info might help anyone that ends up looking at this page after trying to find help with this problem.
Thanks for all the pointers...
Here is my summery..
1) the crash is DEFINATELY due to the SQLITE threading setup in combination with Qt, though dh7892 method is a bit extreme.

Here is what I found works JUST as well, and doesnt require ANY changes to Qt + sqlite

After you open your database, call this function

bool setSingleThreaded( QSqlDatabase & db )
{
QVariant v = db.driver()->handle();
if ( !v.isValid() || qstrcmp( v.typeName(), "sqlite3*" ) != 0 )
{
return false;
}

sqlite3 * handler = *static_cast<sqlite3**>( v.data() );
if ( !handler )
{
return false;
}

sqlite3_config( SQLITE_CONFIG_SINGLETHREAD );
return true;
}

Setting the sql3_config setting to SINGLETHREAD has the same net effect, but is a runtime choice.

Also, what I do to GET the definitions of sqlite3, is add the following to my cmake files

set(qtproject_SRCS
${QTDIR}/src/3rdparty/sqlite/sqlite3.c
)

set(qtproject_H
${QTDIR}/src/3rdparty/sqlite/sqlite3.h
)