目录
1.qt下载安装以及配置环境变量
首先要下载Qt Creator和qt-opensource-windows
下载地址:http://download.qt.io/archive/.
点击qt/
选择5.9/
然后随便选择一个,例如5.9.8/
点击qt-opensource-windows-x86-5.9.8.exe下载
然后退回http://download.qt.io/archive/去下载qt creater
选择qtcreator/
然后随便挑一个,例如qt-creator-opensource-windows-x86_64-4.4.1下载就行
安装教程点击这里:https://blog.csdn.net/xuan_xuan_2/article/details/79514315
下面开始添加环境变量
点击高级系统设置
点击环境变量
点击path,再点击编辑
将E:\softwareinstall\qt_5.9.8\5.9.8\mingw53_32\bin添加在path中即可
qt就下载安装完成了!!!
2. MySQL下载及安装
2.1 MSVG和MinGW的认识
MSVC和MinGW是两种编译器,但是网上的教程大多都是讲解MinGW的,所以一般都是用MinGW进行编译,但是qt里的MinGW是32位的,所以,在下载安装MySQL的时候一定要下载相应的32位的!!(这是第一个坑)
2.2 MySQL下载
首先在MySQL官网下载MySQL5.7.31 installer
链接: https://dev.mysql.com/downloads/installer/
打开后的视图
点击右侧圈起来的就可以看到5.7.31了
下载第二个
之后的安装教程参考:链接:https://blog.csdn.net/qq_45130470/article/details/106211423
真的讲解非常细致,但是有一点要强调一下,如果不想默认装到C盘的同学,请在下面这一步选择Custom,可以自定义MySQL的下载位置和Data下载位置的设置。
2.3 MySQL的彻底删除
方法一:正规方法(狗头)
打开之前下载的安装包mysql-installer-community-5.7.31.0.msi
点击remove
勾选想删除的(可以删掉全部,某几个删除失败就失败吧…),然后next就完事。
方法二:暴力方法
参考这个教程,链接: https://blog.csdn.net/qq_41140741/article/details/81489531
讲的是最好的!!!!
2.4 重装MySQL
对于通过上面正规方法删除后的同学,同样是打开之前下载的安装包mysql-installer-community-5.7.31.0.msi
选择Add
点击edit,注意勾选32-bit,然后点击Filter
在左边框中将文件夹一个一个点开,选中最底层的待下载文件后中间的箭头就变绿了,然后点击箭头,这个文件就放到了右边框里(放置即将下载的文件),重复上面操作将所有左边的文件夹最里面的文件添加到右面的框框中,然后next,就和第一次安装MySQL的过程一样样了。
那对于暴力卸载的同学呢…其实也是一样的(狗头)!!同上…
3. 测试MySQL
3.1 确保打开MySQL服务
首先,要打开任务管理器
确保MySQL57正在运行
3.2 创建数据库并初始化数据
在开始栏中找到MySQL 5.7 Command Line Client(可以把它放到桌面上,比较方便),打开
先输入密码
然后输入:
show databases;
用来显示你的所有数据库
然后输入
create database test;
来创建一个新的空的数据库test,当然用其他名字也可以的
然后输入
use test;
来进入新创建的数据库进行操作
然后输入:
mysql> create table tb_student
-> (
-> num int not null,
-> name varchar(4) not null,
-> score int not null,
-> primary key(num)
-> );
用来创建一个student表
然后输入:
insert into student(num , name, score) values('1','ABC','100');
来插入一个学生的信息
然后输入:
select * from student;
来显示student表
于是乎,我们的第一个数据库就创建完成啦!!
接下来就是如何在qt上对我们创建的数据库进行操作!!
4. qt上操作MySQL
4.1 准备工作(避免报错之QMYSQL driver not loaded)
首先就是一个大坑,在使用Qt操作MySQL数据库过程中,使用Qt编写好连接MySQL数据库的的代码后,编译调试时报错QSqlDatabase: QMYSQL driver not loaded,说明Qt应用程序与MySQL数据库连接失败。
解决方法很简单:
首先打开MySQL安装目录的lib文件夹,将这两个文件复制一下
再找到MinGW编译器的bin文件夹,将这两个文件粘贴进来,就ok啦!
(其实就是MinGW缺少相应的驱动文件,放进来就可以了)
4.2 开始撸码
主要参考博主“技术小白yhx”的文章链接: https://blog.csdn.net/y____xiang/article/details/80412247中的代码
4.2.1 界面设计
打开qtcreater
选择New Project
选择红色圈中内容,然后choose
填写名称之后,选择下一步
这里注意要选择MinGW作为编译器
下一步
完成
首先打开mainwindow.ui,设计界面
注意控件的命名
怎么给控件添加事件呢?
右击控件,选择转到槽
选择OK
便会自动添加槽函数
然后将所有的五个button都转到槽函数
如图所示
4.2.2 代码
首先要在pro文件里添加QT+=sql
接下来,头文件里添加这两行代码
.cpp文件中的代码如下
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
#include <QMessageBox>
#include <QSqlQuery>
#include <QtSql>
#include <QDebug>
#include <QSqlError>
#include <QSqlTableModel>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("1999"); //填写自己的密码
if(!db.open()) {
qDebug() << "error!\n";
} else {
qDebug() << "Open";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButtonInsert_clicked()
{
QString namestr = ui->lineEditName->text();
int num = ui->lineEditNumber->text().toInt();
double score = ui->lineEditScore->text().toDouble();
if(namestr == NULL || num == 0 || ui->lineEditScore == NULL) //检查信息是否填写完整
{
ui->textEdit->setText("请输入完整的信息");
}
else
{
QString str = QString("insert into student(num, name, score) values('%1', '%2', '%3')").arg(num).arg(namestr).arg(score);
QSqlQuery query;
query.exec(str); //执行插入操作
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
ui->textEdit->setText("插入成功");
}
}
void MainWindow::on_pushButtonDelete_clicked()
{
QString name = ui->lineEditName->text();//从行编辑框中获取需要删除的人名
if(name == NULL)
{
ui->textEdit->setText("请输入需要删除的人的名字"); //删除的时候需要输入姓名
}
else
{
//从数据库中查询是否有这个人
QSqlQuery query;
QString temp = QString("select * from student where name = '%1'").arg(name);
query.exec(temp);
QString deletename;
while (query.next())
{
deletename = query.value(1).toString();
}
if(deletename == NULL)
{
QString a = QString("没有叫%1的人,删除失败").arg(name);
ui->textEdit->setText(a);
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
}
else
{
QString str = QString("delete from student where name = '%1'").arg(name);
query.exec(str);//删除信息
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
ui->textEdit->setText("删除成功");
}
}
}
void MainWindow::on_pushButtonSearch_clicked()
{
QString searchname = ui->lineEditName->text();
if(searchname == NULL)
{
ui->textEdit->setText("请输入需要查询的人名");
}
else
{
//从数据库中查询是否有这么一个人
QString str = QString("select *from student where name = '%1'").arg(searchname);
QSqlQuery query;
query.exec(str);
QString name;
int number;
double score;
while (query.next())
{
number = query.value(0).toInt();
name = query.value(1).toString();
score = query.value(2).toDouble();
}
if(name == NULL)
{
QString a = QString("没有叫%1的人,请重新输入人名").arg(searchname);
ui->textEdit->setText(a);
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
}
else
{
ui->lineEditName->setText(name);
ui->lineEditNumber->setText(QString().setNum(number));
ui->lineEditScore->setText(QString().setNum(score));
ui->textEdit->setText("查询成功");
}
}
}
void MainWindow::on_pushButtonSearchAll_clicked()
{
QString name[100];//用来存储从数据库中找出来的信息
int number[100];
double score[100];
int i = 0;
QSqlQuery query;
query.exec("select * from student");//查询所有的信息
while(query.next())
{
number[i] = query.value(0).toInt();
name[i] = query.value(1).toString();
score[i] = query.value(2).toDouble();
i++;
}
ui->textEdit->clear();
int j = 0;
for(j = 0; j < i; j++)//将这些信息都显示在下方的文本编辑框中
{
QString str = QString("学号:%1 姓名:%2 成绩:%3").arg(number[j]).arg(name[j]).arg(score[j]);
ui->textEdit->append(str);
}
}
void MainWindow::on_pushButtonUpdate_clicked()
{
//从界面获取我们需要的信息
QString updatename = ui->lineEditName->text();
int number = ui->lineEditNumber->text().toInt();
double score = ui->lineEditScore->text().toDouble();
if(updatename == NULL || number == 0 || ui->lineEditScore->text() == NULL)
{
ui->textEdit->setText("请输入需要修改的人的学号,姓名以及成绩");
}
else
{
QString temp = QString("select * from student where name = '%1'").arg(updatename);
QSqlQuery query;
query.exec(temp);// 查询信息
QString a;
while (query.next())
{
a = query.value(1).toString();
}
if(a == NULL)
{
QString b = QString("没有名叫%1的人,修改失败").arg(updatename);
ui->textEdit->setText(b);
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
}
else
{
temp = QString("update student set num = '%1' , score = '%2' where name = '%3'").arg(number).arg(score).arg(updatename);
query.exec(temp);
ui->textEdit->setText("修改成功");
ui->lineEditName->clear();
ui->lineEditNumber->clear();
ui->lineEditScore->clear();
}
}
}
4.2.3 运行结果
终于写完了,欢迎评论区讨论、相互解答,大家一起学习进步!!
…