简体   繁体   English

Qt5、C++:未加载 QMYSQL 驱动程序

[英]Qt5, C++: QMYSQL driver not loaded

I am trying to connect to a database using MySQL server with Qt.我正在尝试使用带有 Qt 的 MySQL 服务器连接到数据库。 It is not working and when I run the code, I get the error message below.它不起作用,当我运行代码时,我收到以下错误消息。

QSqlDatabase: QMYSQL driver not loaded QSqlDatabase:未加载 QMYSQL 驱动程序
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7 Unable to connect to the database check your connection details. QSqlDatabase:可用驱动程序:QSQLITE QODBC QODBC3 QPSQL QPSQL7 无法连接到数据库检查您的连接详细信息。

I have been trying this for the past four (4) days now and I do not want to run away from the problem.在过去的四 (4) 天里,我一直在尝试这个,我不想逃避这个问题。 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.我已将文件 libmysql.dll 和 libmysqld.dll 复制到我能找到的每个可能的 bin 中,但没有一个有效。 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. 'MYSQL' 甚至没有被列为我得到的错误行中的可用驱动程序之一,如下所示。 What does that say?那说明什么?

QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7 QSqlDatabase:可用驱动程序: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.我不知道为什么我有这么大的 Qt 尺寸。 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.其中一个显示了实际的文件夹大小,当我单击已安装的 Qt 文件夹时,我在文件夹中看到了 5.12.4 和 5.13.0 版本。 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.这意味着QMYSQL插件不可用或无法成功加载。 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.尝试将QT_DEBUG_PLUGINS 环境变量设置为非零值,并查看可执行文件尝试加载 MySQL 插件时发生的情况。 In most cases, it's because the plugin cannot find the MySQL client libraries as you already mentioned.在大多数情况下,这是因为插件找不到您已经提到的 MySQL 客户端库。

If you are using Qt Creator, note the red arrows from left to right.如果您使用的是 Qt Creator,请注意从左到右的红色箭头。 This is where you can add run environmental variables:您可以在此处添加运行环境变量: 在此处输入图像描述

I do not know why I have such a huge Qt size.我不知道为什么我有这么大的 Qt 尺寸。 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.其中一个显示了实际的文件夹大小,当我单击已安装的 Qt 文件夹时,我在文件夹中看到了 5.12.4 和 5.13.0 版本。 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.除了我在这个阶段知道的这个“QMYSQL”问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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