第一步:下载依赖
1.1去Qt官方下载Qt5.14.2的源代码:
Index of /archive/qt/5.14/5.14.2/singlehttps://download.qt.io/archive/qt/5.14/5.14.2/single/
下载链接:https://download.qt.io/archive/qt/5.14/5.14.2/single/qt-everywhere-src-5.14.2.zip
1.2去Mysql官网下载Mysql SDK
MySQL :: Download MySQL Connector/C (Archived Versions)https://downloads.mysql.com/archives/c-c/
32位SDK下载链接:https://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-win32.ziphttps://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-win32.zip
64位SDK下载链接:
https://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-winx64.ziphttps://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-winx64.zip
第二步:编译和配置
2.1解压mysql-connector-c-6.1.11-win32.zip和mysql-connector-c-6.1.11-winx64.zip,然后新建文件夹mysql,将32位的文件夹命名为x86,将64位的文件夹命名为x64,将其拷贝到mysql文件夹中.
2.2解压qt-everywhere-src-5.14.2.zip,(你的qt源代码目录下)\qtbase\src\plugins\sqldrivers\mysql\,将mysql文件夹拷贝进去,然后双击打开mysql.pro.如下图所示:
2.3打开项目文件之后,如下图所示:
如果你的Desktop Qt 5.14.2 MSVC2017 32bit和Desktop Qt5.14.2 MSVC2017 64bit同时有感叹号,请参考下方文档,进行自定义配置:
Qt 添加MSVC2017编译器(2022年保姆级教程,不安装完整VS)_Copperxcx的博客-CSDN博客_msvc2017https://blog.csdn.net/Copperxcx/article/details/122540629?spm=1001.2014.3001.5506我的配置仅供参考:
2.4配置好了之后打开,如下图所示:
修改mysql.pro文件参数为下图:
TARGET = qsqlmysql
HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp
#QMAKE_USE += mysql
OTHER_FILES += mysql.json
PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
include(../qsqldriverbase.pri)
greaterThan(QT_MAJOR_VERSION, 4) {
TARGET_ARCH=$${QT_ARCH}
} else {
TARGET_ARCH=$${QMAKE_HOST.arch}
}
contains(TARGET_ARCH, x86_64) {
#ARCHITECTURE = x64
INCLUDEPATH += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x64/include"
DEPENDPATH += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x64/include"
LIBS += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x64/lib/libmysql.lib"
} else {
#ARCHITECTURE = x86
INCLUDEPATH += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x86/include"
DEPENDPATH += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x86/include"
LIBS += "D:/Software/Qt/5.14.2/Src/qtbase/src/plugins/sqldrivers/mysql/mysql/x86/lib/libmysql.lib"
}
上述的INCLUDEPATH,DEPENDPATH和LIBS根据自己的路径进行设置.
2.5点击QtCreator上方的菜单栏中的构建(B)->重新构建所有项目.(此处只是生成了,Windows下的32位的Debug和Release的dll,你也可以生成Windows64位,MinGW32位和64位的库,只需要点击左下角小电脑更改编译器即可).
2.6在D盘根目录找到plugins\sqldrivers\qsqlmysqld.dll和qsqlmysql.dll(如果找不到那么就装个everything进行搜索吧)将其分别拷贝到你的Qt安装目录下的msvc2017\plugins\sqldrivers\(这是32位的目录),如果是64位的需要拷贝到msvc2017_64\plugins\sqldrivers\.
2.7找到mysql文件夹,将libmysql.dll拷贝到msvc2017\bin目录下.
自此所有编译和配置操作完毕,接下来进行测试!
第三步:测试
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlRecord>
#include <QtSql/QSqlField>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtWidgetsApplication1 w;
w.show();
//查看Qt支持的数据库
qDebug() << QSqlDatabase::drivers();
QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName("192.168.211.128"); //设置主机地址
database.setPort(3306); //设置端口 默认3306
database.setDatabaseName("table_one"); //设置数据库名称
database.setUserName("test"); //对应数据库的用户名
database.setPassword("123456");//对应数据库的密码
if (!database.open())
qDebug() << "failed" << database.lastError().text();
else
qDebug() << "success";
QSqlQuery query;
query.prepare("select * from hardware");
query.exec();
while (query.next())
{
QSqlRecord record = query.record();
for (int i = 0; i < record.count(); ++i)
{
qDebug() << record.fieldName(i) << "->" << record.value(i).toString();
}
printf("\n");
}
database.close();
return a.exec();
}
运行结果:
如果你的Mysql数据连不上,报错SSL connection error: unknown error number,参考链接:
QT连接mysql:实现5.9.0的32位QT连接8.0的64位mysql并且解决SSL“SSL connection error: unknown error number QMYSQL错误_Nova_SSS的博客-CSDN博客https://blog.csdn.net/Nova_SSS/article/details/125379858?spm=1001.2014.3001.5506
已编译好的分享给大家,链接地址:
Qt5.14.2插件qsqlmysql.dll-C++文档类资源-CSDN文库https://download.csdn.net/download/qq_31629063/86899370