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

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.

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

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!" );

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

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.

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