Mysql官网地址–> MySQL Community Server -->
下载Windows (x86,64bit)ZIP
2.2 配置my.ini文件我这准备安装在
D:\uninstall\mysql-8.0.29-winx64
目录
在D:\uninstall\mysql-8.0.29-winx64\bin
目录创建my.ini文件,内容如下:
注意:basedir=D:\uninstall\mysql-8.0.29-winx64
这个目录是mysql文件的目录(此处需根据文件路径更改)
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=D:\uninstall\mysql-8.0.29-winx64
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
3. 初始化及启动mysql服务
- 以管理员的方式打开cmd命令窗口(直接打开cmd运行,可能会报错),并且进入到mysql安装目录的bin目录下。
cd D:\uninstall\mysql-8.0.29-winx64\bin
.\mysqld -install
- 初始化
.\mysqld --initialize
- 启动mysql服务
net start mysql
4. 测试是否能连接到mysql数据库
此处会让输入密码
Enter password:
, 密码为初始化服务时自动生成,存在于"D:\uninstall\mysql-8.0.29-winx64\data\xxxx.err"
文件中,打开文件查找password,可以看到初始密码为:-cmRwxffH1Pc
.\mysql -uroot -p
出现如下界面代码mysql安装成功:
注:
修改数据密码root:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
5. 使用Qt编译mysql的驱动
- 找到qt源代码目录下得mysql.pro,
- 使用Qt Creator打开,构建报错
- 修改qsqldriverbase/qsqldriverbase.pri
注释第4行的:
include($$shadowed($$PWD)/qtsqldrivers-config.pri)
添加:include(./configure.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
- 修改mysql.pro文件
(1) 注释:# QMAKE_USE += mysql
(2) 添加如下内容:
LIBS += D:\uninstall\mysql-8.0.29-winx64\lib\libmysql.lib
INCLUDEPATH += D:\uninstall\mysql-8.0.29-winx64\include
DEPENDPATH += D:\uninstall\mysql-8.0.29-winx64\include
DESTDIR=../mysql/driver
修改后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:\uninstall\mysql-8.0.29-winx64\lib\libmysql.lib
INCLUDEPATH += D:\uninstall\mysql-8.0.29-winx64\include
DEPENDPATH += D:\uninstall\mysql-8.0.29-winx64\include
DESTDIR=../mysql/driver
6.构建生成
生成的文件在mysql.pro同级目录的driver文件夹中
(1) 拷贝qsqlmysql.dll和qsqlmysqld.dll到D:\install\Qt.15.2\msvc2019_64\plugins\sqldrivers
目录下
(2) 拷贝D:\uninstall\mysql-8.0.29-winx64\lib
目录下的libmysql.dll,到D:\install\Qt.15.2\msvc2019_64\bin
目录
需要先使用终端连接到mysql数据库:
use mysqll create database test; show databases;
确定存在一个test的数据库即可
新建一个console的qt项目,需改.pro文件:
QT += core gui sql
,添加sql模块
main文件内容如下:
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
// 输出可用数据库
qDebug() << "Available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach (QString driver, drivers)
qDebug() << driver;
// 打开MySQL
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("root");
if (!db.open())
qDebug() << "Failed to connect to mysql";
else
qDebug() << "Success to connect to mysql";
QSqlQuery query(db);
//注意这里varchar一定要指定长度,不然会出错
query.exec("create table Stu(id int primary key,name varchar(20))");
query.exec("insert into Stu values(1,'student1')");
query.exec("insert into Stu values(2,'student2')");
query.exec("insert into Stu values(3,'student3')");
query.exec("select id,name from Stu where id >= 2");
while (query.next()) {
int value0 = query.value(0).toInt();
QString value1 = query.value(1).toString();
qDebug() << value0 << value1;
}
return a.exec();
}
输出内容如下代表成功:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)