Qt sqlite issue at deployement : driver not loaded - c++

As the title says, I've been having this issue for some days now. My program runs fine in debug mode on QtCreator, but once I choose release mode, I get the sql driver not loaded issue (I've only been running it on windows 10). I believe my .pro file is correctly written (QT += sql is written).
I've tried most things I could :
windeployqt.exe .
move plugins myself in sqldrivers sub-folder in executable folder containing all sqldrivers provided by Qt installation path and also moved at executable folder Qt5Sql.dll
I also tried using sqlite3.dll provided by sqlite website and moving it in both (one by one tested) executable folder and sqldrivers sub-folder.
As I said the issue happens only at deployement so I am wondering if there is something I should have added to my .pro file.
I don't have any sql program installed on my windows OS. If that is the issue, which I think is, I was wondering how I could force my program to use plugins provided in sqldrivers sub-folder.
Errors :
QSqlDatabase: QSQLITE driver not loaded
QSqlDatabase: available drivers:
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
QSqlQuery::exec: database not open
EDIT : I managed to fix that. The issue came from something I didn't expect. I was using global variables and one of them was my database, I realized it by reading again the error as it seemed the database was loaded before the first lines of main.cpp (at #include). So right now I'm using opening and closing the database each time I use it. Is there some way I could declare a global database (keep it open all the time) ? I'm using it quite intensively.

Yes, you can open your database once and then just use static public methods of the QSqlDatabase class:
QSqlDatabase my_db = QSqlDatabase::addDatabase("QSQLITE");
my_db.setDatabaseName("my_db_name.sqlite");
if(my_db.open())
{
my_db.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
}
else
{
qDebug() << my_db.lastError().text();
}
And then in another place:
QSqlDatabase db = QSqlDatabase::database();
if(db.isOpen())
{
qDebug() << "Wow";
db.exec("insert into person values(101, 'Danny', 'Young')");
}
See QSqlDatabase::addDatabase(), and use the parameter connectionName if necessary.

Related

QSqlDatabase: QSQLITE driver not loaded - Only in Debug Mode

I am attempting within my code to connect to an SQLite database:
bool MainWindow::connOpen(){
mydb=QSqlDatabase::addDatabase("QSQLITE");
//qDebug ( ) << QSqlDatabase::drivers();
QString dbpath = "dbname.sqlite";
mydb.setDatabaseName(dbpath);
}
which gives me the Error Message: "QSqlDatabase: QSQLITE driver not loaded"
as well as an error window "Driver not Loaded Driver not Loaded". The QSqlDatabase mydb declaration is in the MainWindow header file.
Strangely, this only happens in Debug Mode, in Release Mode everything ist fine. Even weirder, this used to work before (I think) an automatic QT Update. I am using QTCreator 4.4.1 and QT 5.9.2. Also, I checked, the sqlite.dll is within the sqldrivers folders, where I understand it should be. But for some reason the Qt Folder is called QT 5.9.1 unlike my actual version, only this does not seem to have any effect. Everything else works fine.
Also, when I uncomment the QSqlDatabase::drivers(); line, the output in Debug Mode is (), while in Release Mode I get ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7"). Clearly, the drivers are not being found.
Does anybody have an idea where the difference betweeen debug and release could come from? Thanks a lot!
To summarize as an answer:
The fact that release builds work, but debug fails is because on windows there are actually 2 files per plugin - in this case qsqlite.dll and qsqlited.dll. The one with the d is used for debug builds, and the other one for release builds.
As the debug variant is missing, a reinstallation of Qt is the only way to get back the missing files.
In my case:
So does not work:
db = QSqlDatabase::addDatabase("QSQLITE", "MyConnection"); //MyConnection as connection name - Driver not loaded
and it works:
db = QSqlDatabase::addDatabase("QSQLITE");
Both versions work in Release.
QT 5.10

Building MySQL plugin for QT 5.7

So I'm try to build the MySQL plugin for qt 5.7 on Windows. I managed to generate the qmysqld.dll an qmysql.dll by doing the following
1) Downloading Qt source files
2) Navagating to QTDIR\qtbase\src\plugins\sqldrivers and running qmake on the qsqldriver.pro file while linking both mySql\include and mySql\libmysql.lib. Specifically I ran the following command:
qmake "INCLUDE+=mysql-5.7.17-win32\include" "LIBS+=C:\mysql-5.7.17-win32\lib\libmysql.lib" -o MakeFile mysql.pro
3) Running mingw32-make to compile the project.
4)I then moved both qmysqld.dll and qmysql.dll from QTDIR\plugins\sqldrivers to ACTUALQTINSTALLATION\mingw53_32\plugins\sqldrivers
The files were already there and needed to be replaced.
This is the test code I'm trying to run.
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "my_sql_db");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("");
if(!db.open())
qDebug() << db.lastError().text();
Which produces the following error, as it did before I built the plugins.
"Driver not loaded Driver not loaded"
Driver: ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
The only thing I can think of is that I ran the DependencyWalker program on qsqlmysqld.dll and it's saying that LIBMYSQL.dll, QT5CORED.dll and QT5SQLD.dll are missing and that pretty much every other dependency is for an x64 bit architecture(As opposed to qsqlmysql.dll which is 32). I know very little about what that means and it's my first time using DependencyWalker but would this indicate that I've somehow built the dll to be 32 bit and need to recompile it as a 64 bit and that should solve the issue?
Very confused about this, I've been trying to get this to work on and off for over a month now.

Qt deployed (macdeployqt) application does not create a file

I'm facing the following problem:
I wrote a simple application to track working hours. Therefor I create a *.db file programmatically.
Launching the application from Qt Creator (debug or release) works perfectly fine.
I used the macdeployqt tool to get a *.dmg file. When starting the application now the method (see below) cannot open, respectively create the *.db file and I run out of ideas why. In addition to this, the application should output some *.csv files. This also fails.
One more information, running the application as administrator using the sudo command on terminal...well it works and the *.db file and *.csv files get created.
So I am quite sure it must deal with file permissions but I have no idea how to change this except for changing it in the information context menu but this didn't help at all.
Below the method for the *.db file which always returns false when not launching the app from Qt Creator:
QFile file(Globals::Environment::WORKING_DIRECTORY + "/" + "Records.db");
if(file.open(QIODevice::ReadWrite))
return true;
else
{
QMessageBox msg;
msg.setWindowTitle("Error");
msg.setText("Failed to create database file!");
msg.exec();
}
return false;
I am on MacOS 10.11.3.
Qt 5.5.1 (Clang 6.1 (Apple), 64 bit)
If more information is needed, I will provide it of course.
Thanks a lot for every help in advance.

Getting error "QSQLITE driver not loaded" when running Qt program in Virtual Machine

I have a Qt4 program, which only opens a Qt database:
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(fileName);
if (!db.open()) {
qDebug() << db.lastError().text();
}
I run the program from Explorer, and inside the directory I have only the exe and the DLLs it needs: Test.exe
QtCore4.dll
QtSql4.dll
QtGui4.dll
The same exe and DLLs work when executed on my own computer, but fail when executed in a Virtual Machine with these errors:
[2784] QSqlDatabase: QSQLITE driver not loaded
[2784] QSqlDatabase: available drivers:
And db.lastError().text() returns "Driver not loaded Driver not loaded" (yes, it's repeated twice).
Why is it working on my computer? If there is a dependency missing, I assume it would fail on my own computer too, because it looks only in its own directory for all the DLLs that it needs. Obviously there is a dependency that it finds on my computer, but not on the virtual machine, and it must be looking for it elsewhere (not only in the directory where the exe is).
You should also place qsqlite4.dll in a directory named sqldrivers alongside the executable.

Qt ODBC driver not loaded

I've got the following problem with QODBC driver:
bool Dialog::createOdbcConnection(QSqlDatabase * db, QString odbcName,QString user,QString pass)
{
db = new QSqlDatabase();
db->addDatabase("QODBC");
db->setDatabaseName(odbcName);
if(!user.isEmpty())
db->setUserName(user);
if(!pass.isEmpty())
db->setPassword(pass);
qDebug() << QSqlDatabase :: drivers();
if (!db->open())
{
QMessageBox mgs;
qDebug() << db->lastError().text();
mgs.setText(db->lastError().text());
mgs.exec();
return false;
}
return true;
}
qDebug() << QSqlDatabase :: drivers(); returns ("QSQLITE", "QODBC3", "QODBC"), but the program doesn't open my database, db->open() returns false and the error is "Driver not loaded Driver not loaded"
Whats the use of the QSqlDatabase Parameter in your createOdbcConnection-Method?
I would rather remove it from there, define a QSqlDatabase Object in your Class-Definition:
private:
QSqlDatabase db_;
And initialize it in your Class-Constructor:
db_ = QSqlDatabase::addDatabase("QODBC");
That should work!
I am seeing this problem is resolved, but I am adding the notes in case someone might be going into the same troubles as mine and looking for a solution when the driver is not loaded, but the described solution is not enough.
It depends on where you compile and intend to use your ODBC-related Qt code. I ran into similar issues.
My code was working perfectly on Windows, but returning an error when compiled elsewhere (Linux).
When running the compiled code on Linux you will get into trouble because the driver, libsqlodbc.so , even if it exists in the /plugins/sqldriver directory , it depends on some particular library which has to be installed independently.
you can see which library is missing by
ldd ./path-to-libsqlodbc.so/libsqlodbc.so
you can see if any other library is missing to make your binary file run by
ldd ./path-to-your-binary-file/name-of-your-binary-file
Use this information to install the ODBC on your Linux (if you need so):
installing odbc driver on linux