Reputation: 137
I have an application that is using SQLite within Qt using QSqlDatabase and everything works just fine if I compile in Debug mode. My problem is that, when I switch to Release mode nothing works. My first errors seemed to be related to the code bringing in the QtCore4.dll and QtGui4.dll files. Once I moved those files into the same directory as the compiled code, the program then loaded but the following error was written to the Qt Creator console:
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
The only line that I think this could be from is this one which I just have at the top to make it global:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
My PRO file is as follows:
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = LessonsLearned
TEMPLATE = app
SOURCES += main.cpp\
lessonslearned.cpp
HEADERS += lessonslearned.h
FORMS += lessonslearned.ui
I've even tried reinstalling Qt to see if that fixes the problem.
Just to reiterate...everything works just fine if I compile Debug. It's Release that the problems appear.
I'm using Qt 4.7.3 and Visual Studio 2008 in case there's some sort of reason that might cause the issue.
UPDATE
I've moved the declaration of "QSqlDatabase db" inside the header file for the class and have it as private. I've also moved the call to addDatabase("QSQLITE") to the constructor:
LessonsLearned::LessonsLearned(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LessonsLearned)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE");
}
This made the error about needing QCoreApplication dissappear, but I still get the first two messages. Still works in Debug but not Release. I'm wondering if the program is still pointing to a wrong DLL somewhere. I've tried dependency walker some more, but I haven't been able to find one that's wrong, yet.
UPDATE 2 I found something helpful here: QSQLITE driver not loaded - where to put qt database driver plugins. I think this is the same issue that I had. I still don't know why I had to modify the Release directory to add a sqldrivers folder, but it works.
Upvotes: 2
Views: 4619
Reputation: 2290
For the release problem:
If bringing QtCode4.dll
and QtGui4.dll
solved some of the problems then probably bringing the qsqlite4.dll
would solve the remaining issue. Just note that you have to put it in a folder named sqldrivers
near the executable file.
Upvotes: 3
Reputation: 5466
Probably your issue is that you have a global variable: QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); That means addDatabase() will get called before main(), which means before the QCoreApplication object is created.
So you can only call addDatabase() after you created your QCoreApplication object (which is usually one of the first things in main()).
Avoid global variables, especially ones with complex types and even more so ones that call functions.
I don't know why you only get the problem in release mode though.
Upvotes: 2