I've created a Qt dynamic lib that uses Qt SQL to open an SQLite database, but I'm getting this error:
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
The DLL was working fine as part of a Qt Android application, however I need to use it through JNI from an existing Java application developed in Eclipse.
This is the shortest example code that reproduces the problem. I load the library from Java and call its init()
method:
System.loadLibrary("plugins_sqldrivers_libqsqlite");
System.loadLibrary("Qt5Sql");
System.loadLibrary("MyQtLib");
MyQtLib.init();
And inside the Qt library I just call QSqlDatabase::addDatabase():
JNIEXPORT void JNICALL Java_test_MyQtLib_foo(JNIEnv *, jclass)
{
// Manually create a QCoreApplication instance.
int argc = 1;
static char arg[] = "";
static char *arg2 = arg;
app = new QCoreApplication(argc, &arg2);
// Try to add an SQLite db connection.
QSqlDatabase::addDatabase("QSQLITE");
}
Since the error is QSQLITE driver not loaded
, and the Qt library was working inside a Qt application, I assume that Qt is doing some initialization that I'm missing.
But this didn't remove the error, so it must be something else. Normally, the Qt application will use QtApplication.java and QtActivity.java to perform some initialization, so they must be doing something more there that I'm not doing.