How to fix "driver not loaded" using QODBC in Visual Studio - c++

I'm working on a project where I need to load a database using QT, and I've built the QODBC plugin using the method provided online, but I still get the "Driver not Loaded" error when trying to access the DB.
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=C:\\dbs\\project.mdb");
if(db.open())
QMessageBox::warning(this, "opened","successful");
else
QMessageBox::warning(this, "opened", (db.lastError().text() + "\n" + db.driverName()));
I have a feeling I'm not including the plugin or something, but I'm not sure how to fix this.

Related

Qt QSqlDatabase QSQLITE driver not loaded error

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();
}

(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.

Qt cannot load MySql driver on XAMPP

I have written a very simple test to see how database connection works. This is my first attempt :
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("testqt");
db.setUserName("aaaaaa");
db.setPassword(".bbbbbb!,");
if (db.open())
{
qDebug() << "Connected!";
}else
{
qDebug() << "Failed to connect!";
}
Why I always get this error?
Note that I have this :
I have also seen online that someone puts the libmysql.dll and I've done that but still nothing. I have installed the C and C++ mysql connector from internet. Any idea?
My SQL database is on a XAMPP server and this is the username :
Also note this:
There is mariadb but it asks for libmysql!
If you have using of MinGW compiler, download c connector from bellow link :
https://dev.mysql.com/downloads/connector/c/
Then copy "libmysql.dll" dll file to your qt path where installed :
C:\Qt\5.5\mingw492_32\bin

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.

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