Qt QSqlDatabase QSQLITE driver not loaded error - c++

I am getting the driver not loaded error when trying to open an sqlite
database with Qt 5.15.0 on Windows.
Here is my output: QSqlDatabase: QSQLITE driver not loaded QSqlDatabase: available drivers: QSQLITE QPSQL QPSQL7 QODBC QODBC3
"Driver not loaded Driver not loaded"
Here are the files in the directory with my .exe (currently and for
this output): main.o app.exe qrc_qml.cpp qrc_qml.o
I have done some searches on this topic and they have suggested the
following:
manually move the sqldrivers folder to the exe directory or try moving its contents to the directory, neither have resolved my issue,
output remains the same
use windeployqt. I have tried this but despite it moving the dlls including moving the sqldrivers folder to the exe directory, the same
error appears
Please let me know what you suggest
QSqlDatabase my_db = QSqlDatabase::addDatabase("QSQLITE");
my_db.setDatabaseName("test.db");
if(my_db.open())
{
qDebug() << "open";
}
else
{
qDebug() << my_db.lastError().text();
}

Related

(macOS High Sierra, Qt C++) ERROR: "QSqlDatabase: QMYSQL driver not loaded"

I'm struggling for a few days to connect my Qt project to MySql database without success. There are similar problems explained out there (eg. LINK1, LINK2, LINK3) but they didn't help me to solve my problem.
I use:
macOS High Sierra
XAMPP 7.4.4,
Qt Creator 5.14.2
I also set up my database using phpmyadmin. I also used: "brew install mysql-client" and it was successful.
I did a simple qt project that is supposed to connect to my database.
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost"); //127.0.0.1
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("monitoringDB");
if (!db.open()) {
qDebug() << "Couldn't open db";
} else {
qDebug() << "DB opened!";
}
However when I try to run it I get the following error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
I read much about the problem and I understand that I should changed dynamic shared libraries using install_name_tool but unfortunately I can't find on my computer any libqsqlmysql.dylib file that would need to be updated.. However I can find libmysqlclient.21.dylib
I would be very grateful if you have some suggestions because I think I'm really out of ideas.

QMYSQL Driver not loaded using Qt 5.12.0 in Ubuntu 16.04

Hi I'm trying to build a program that connect on MySQL, i have this error
"QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7"
But i already "libmysqlclient.so.20" and "libqsqlmysql.so" in my sqldrivers folder. Please do help me, I'm a beginner in QT Programming. Sorry for my grammar.
I'm using this code
QSqlDatabase db1 = QSqlDatabase::addDatabase( "QMYSQL" );
db1.setHostName("127.0.0.1");
db1.setPort(8080);
db1.setDatabaseName( "sample" );
db1.setUserName( "root" );
db1.setPassword( "" );
if( !db1.open() )
{
qDebug() << db1.lastError();
qFatal( "Failed to connect." );
}
qDebug( "Connected!" );

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

Qt sqlite issue at deployement : driver not loaded

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.

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.