打开Visual Studio Installer,如果已经安装过内容了可能是如下页面,点击修改(头一回打开的话不需要这一步):
然后在工作负荷中勾选使用C++的桌面开发,它会帮我们勾选好一些组件,以及一个Windows 10 SDK:
在单个组件中勾选自己要安装的编译器,此处安装了MSVC2015、MSVC2017、MSVC2019:
检查mingw73_64/plugins/sqldrivers目录下是否存在qsqlmysql.dll文件,没有就需要编译mysql驱动。
2.在项目中,选择需要构建的源目录
附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/
进入文件夹D:\Library\Qt5.14.2\5.14.2\msvc2017_64\mkspecs\common,打开msvc-version.conf文件,添加
QMAKE_MSC_VER = 1900,对应如下:
将#QMAKE_USE += mysql这一行注释掉
一定要复制到mysql对应位数的编译器中,然后在构建的时候,注意选择需要生成的应用是32位还是64位。
6.DESTDIR 定义编译后lib的路径,寻找编译后的数据
7.将文件复制到编译器的5.14.2\msvc2017_64\plugins\sqldrivers中
把5.14.2\msvc2017_64\plugins\sqldrivers文件夹全部拷贝到项目的执行目录下。
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
#include
#include
#include
#include
#include
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....");
}