简体   繁体   中英

Qt5, C++: QMYSQL driver not loaded

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安装尺寸

Qt文件夹内的两个版本

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: 在此处输入图像描述

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安装尺寸

Here is the second image. Could it be there was a problem with my installation? But everything is working fine with it, Qt文件夹内的两个版本 except this 'QMYSQL' issue that I know of at this stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM