0
votes

I am trying to connect to a database using MySQL server with Qt. It is not working and when I run the code, I get the error message below.

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7 Unable to connect to the database check your connection details.

I have been trying this for the past four (4) days now and I do not want to run away from the problem. So, I will really appreciate anyone's help. I have looked at several similar post but none has helped me. I have copied the files libmysql.dll and libmysqld.dll to every possible bin that I could find and none of that worked. I notice a big difference from my error message and the ones that other got. 'MYSQL' is not even listed as one of the available drivers in the error line that I got which is below. What does that say?

QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7

#*******This is my main.cpp file**********

#include <QCoreApplication>
#include <QtSql/QtSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>

//Adding the i/o stream
#include <iostream>
using namespace std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

//Creating a database connection
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setUserName("root");
db.setPassword("");
db.setDatabaseName("qtdatabase");

//Testing the connection
if(db.open()) {
    cout << "Database connected successfully" << endl;

} else {

    cout << "Unable to connect to the database check your connection details." << endl;
}


    return a.exec();
}


#********This is my mysqlconnect.pro file**********

QT += core
QT += sql
QT -= gui

CONFIG += c++11 console

TARGET = mysqlconnect

CONFIG -= app_bundle


SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target



win32: LIBS += -L$$PWD/../../../../Users/user/Desktop/lib/ -llibmysql

INCLUDEPATH += $$PWD/../../../../Users/user/Desktop
DEPENDPATH += $$PWD/../../../../Users/user/Desktop


I do not know why I have such a huge Qt size. You can see it in the attached images. One shows the actual folder size, and when I click on the installed Qt folder, I see both 5.12.4 and 5.13.0 versions in the folder. Is that normal? I have attached the images here please.Qt installation size

The two versions inside the Qt folder

1

1 Answers

1
votes

It means that the QMYSQL plugin is not available or it could not be loaded successfully. Try to set QT_DEBUG_PLUGINS environment variable to a non-zero value and see what is happening while your executable is trying to load MySQL plugin. In most cases, it's because the plugin cannot find the MySQL client libraries as you already mentioned.

If you are using Qt Creator, note the red arrows from left to right. This is where you can add run environmental variables: enter image description here