安装MSVC编译器与Windows 10 SDK
打开Visual Studio Installer,如果已经安装过内容了可能是如下页面,点击修改(头一回打开的话不需要这一步):
然后在工作负荷中勾选使用C++的桌面开发,它会帮我们勾选好一些组件,以及一个Windows 10 SDK:
选择win10 sdk
在单个组件中勾选自己要安装的编译器,此处安装了MSVC2015、MSVC2017、MSVC2019:
QCreator编译mysql dll
1.查看当前QT在msvc2017_64下的数据库插件类型
检查mingw73_64/plugins/sqldrivers目录下是否存在qsqlmysql.dll文件,没有就需要编译mysql驱动。
2.在项目中,选择需要构建的源目录
3.点击编辑,打开mysql.pro文件进行配置
附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)
LIBS += "D:\Library\mysql-5.7.32-winx64\lib\libmysql.lib"
INCLUDEPATH += "D:\Library\mysql-5.7.32-winx64\include"
DESTDIR = ../mysql/qtMyLib
附qsqldriverbase.pri文件:
QT = core core-private sql-private
# For QMAKE_USE in the parent projects.
#include($$shadowed($$PWD)/qtsqldrivers-config.pri)
include(./configure.pri)
PLUGIN_TYPE = sqldrivers
load(qt_plugin)
DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
4.下载mysql对应版本的内容
https://downloads.mysql.com/archives/community/
解压如下:
5.进行编译
先点锤子,再点执行,出现如下图片即成功:
(1)出现错误:QMAKE_MSC_VER isn’t set
进入文件夹D:\Library\Qt5.14.2\5.14.2\msvc2017_64\mkspecs\common,打开msvc-version.conf文件,添加
QMAKE_MSC_VER = 1900,对应如下:
(2)出现错误:Library ‘mysql’ is not defined,在qt左侧栏中双击打开mysql.pro
将#QMAKE_USE += mysql这一行注释掉
(3)mysql和编译器的位数要一致,mysql是64位,那么上述三个文件都要复制到64位的编译器中
一定要复制到mysql对应位数的编译器中,然后在构建的时候,注意选择需要生成的应用是32位还是64位。
6.DESTDIR 定义编译后lib的路径,寻找编译后的数据
7.将文件复制到编译器的5.14.2\msvc2017_64\plugins\sqldrivers中
8.将mysql的dll复制到项目的执行目录下
(4)QSqlDatabase::drivers 返回空
把5.14.2\msvc2017_64\plugins\sqldrivers文件夹全部拷贝到项目的执行目录下。
QSqlDatabase集成mysql
1.cmakelist添加SQL依赖
find_package(Qt5 COMPONENTS
Core
Gui
Widgets
Sql
REQUIRED)
add_executable(QtDemo2 main.cpp
sql_process.h
sql_process.cpp)
target_link_libraries(QtDemo2
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Sql
)
2.新建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` VARCHAR(64) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`permission` INT(2) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
UNIQUE INDEX `username`(`username`) USING BTREE
) ENGINE = INNODB AUTO_INCREMENT = 40 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES (1, 'admin', '123456', 1);
INSERT INTO `users` VALUES (32, 'xfgc', '肖1111', 2);
INSERT INTO `users` VALUES (39, '123', '哲222', 1);
SET FOREIGN_KEY_CHECKS = 1;
3.sql常用操作
//
// Created by zhe.xiao on 2023/11/22.
//
#include "sql_process.h"
#include <QSqlDatabase>
#include <QStringlist>
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlRecord>
void SqlProcess::connect()
{
qDebug("SqlProcess started....");
QSqlDatabase dbLink = QSqlDatabase::addDatabase("QMYSQL");
dbLink.setPort(3306);
dbLink.setDatabaseName("ws300");
dbLink.setHostName("localhost"); //连接本地主机
dbLink.setUserName("root");
dbLink.setPassword("Xi1ozh#1");
if (!dbLink.open())
{
qDebug("SqlProcess QSqlDatabase Database Open Failed!!");
}
QSqlQuery query(dbLink);
QString sql = QString("select * from users;");
if (query.exec(sql))
{
qDebug("SqlProcess QSqlQuery Succceed!!");
int fieldNo = query.record().indexOf("password");
while (query.next())
{
std::string a1 = query.value(fieldNo).toString().toStdString();
std::string b1 = query.value(1).toString().toStdString();
std::string c1 = query.value(2).toString().toStdString();
std::string d1 = query.value(3).toString().toStdString();
qDebug("SqlProcess QSqlQuery Data %s %s %s %s", a1.c_str(), b1.c_str(), c1.c_str(), d1.c_str());
}
}
dbLink.close();
qDebug("SqlProcess finished....");
}