SQLite: proper method for calling sqlite3 directly?
-
@davecotter said in SQLite: proper method for calling sqlite3 directly?:
can't i link directly to the existing sqlite3 that QSqlDatabase already provides?
Unfortunately not. The SQLite library itself is statically linked (embedded) into
qsqlite.dll
(or .so or .dylib) so the raw SQLite functions are no longer callable.This contrasts with the PostgreSQL plugin:
qsqlpsql.dll
is simply a small wrapper that dynamically links to the raw PSQL library, so your app can link to it directly and bypass the Qt wrapper if you wish. This also means you must remember to bundle the PSQL library when you distribute your application.I have tried instead just compiling sqlite3 into my app, but that seems AWEFULLY wasteful of space to include the lib again for JUST ONE CALL.
It's kind of like bundling a whole dynamic library with your app just to call one function from the library.
-
It's kind of like bundling a whole dynamic library with your app just to call one function from the library.
right, but... it sounds like that's exactly what i must do???
i'm surprised the SQLite impl in Qt doesn't provide forwarding function wrappers for things like this?
-
@davecotter said in SQLite: proper method for calling sqlite3 directly?:
i'm surprised the SQLite impl in Qt doesn't provide forwarding function wrappers for things like this?
There are some database-specific settings available: https://doc.qt.io/qt-5/qsqldatabase.html#setConnectOptions You can set parameters like
QSQLITE_BUSY_TIMEOUT
but that doesn't let you register callback functions.If you want, you can present a case for including this kind of functionality by posting at https://bugreports.qt.io/
-
@eyllanesc This does not work. You then have two different static sqlite instances which will crash sooner or later.
-
@Christian-Ehrlicher Are you sure? I have used that method many times and have never had a problem. The only thing I forgot to point out in my post was that after obtaining the handle:
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());
then thesqlite3_initialize()
function must be called.If you check the way sqlite is included in the plugin you will see that it is not linked statically or dynamically but as files (the .h and .c are added): https://github.com/qt/qtbase/blob/5.15.0/src/plugins/sqldrivers/sqlite/sqlite.pro . It is only dynamically linked if the "system-sqlite" setting is enabled.
-
@eyllanesc said in SQLite: proper method for calling sqlite3 directly?:
then the sqlite3_initialize() function must be called.
And that's exactly the problem - you then have two different instances running. sqlite3_initialize() creates a local storage for some stuff (see the sources)
-
A quick look at the Qt Sqlite plugin shows that it can be built against a system Sqlite (--system-sqlite).
Does this result in a static copy of Sqlite embedded in the plugin or does it dynamically link? If it is the latter, then you should be able to link to the same dynamic library.
-
Since the system sqlite is a shared lib - no.
-
so i ask again: can i use the built in sqlite plugin, the one that comes with the install of Qt, and link against the sqlite.cpp code? or must i build it myself? if the latter: is there a "for dummies" step by step wiki about exactly how to build it?
-
Hi,
It's in the documentation.