QTday4
数据库头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QSqlDatabase> //数据库管理类
#include<QSqlQuery> //数据库查询类
#include<QSqlRecord> //记录类
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_addBtn_clicked();
void on_deleteBtn_clicked();
void on_showBtn_clicked();
private:
Ui::Widget *ui;
//实例化一个数据库对象
QSqlDatabase db;
};
#endif // WIDGET_H
数据库源文件
#include "widget.h"
#include "ui_widget.h"
#include<QMessageBox> //消息对话框
#include<QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//想要添加某个数据库
if(!db.contains("stu.db"))
{
//如果当前对象中没有包含所需的数据库,则添加一个数据库
db = QSqlDatabase::addDatabase("QSQLITE"); //添加一个sqlite3的数据库
db.setDatabaseName("stu.db"); //设置数据库的名称
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this, "提示", "数据库打开失败");
return;
}
//程序执行至此,表示数据库创建成功并打开了
//准备创建数据表
//1、实例化一个sql语句的执行者
QSqlQuery querry;
//2、准备sql语句
QString sql = "create table if not exists STU(id int, name char, sex char, score double);";
//3、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this, "提示", "数据表创建失败");
return;
}
//程序执行至此,表示数据表也创建成功了
}
Widget::~Widget()
{
delete ui;
}
//添加信息按钮对应的槽函数
void Widget::on_addBtn_clicked()
{
//1、将ui界面上的相关信息获取下来
int ui_numb = ui->numEdit->text().toUInt(); //获取学号
QString ui_name = ui->nameEdit->text(); //姓名
QString ui_sex = ui->sexEdit->text(); //性别
double ui_score = ui->scoreEdit->text().toDouble(); //分数
//2、判断信息中是否有空的
if(ui_numb==0 || ui_name.isEmpty() || ui_sex.isEmpty() || ui_score==0)
{
QMessageBox::information(this,"提示","请将信息填写完整");
return;
}
//3、准备sql语句
QString sql = QString("insert into STU(id, name, sex, score) values(%1, '%2', '%3', %4);")
.arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);
//qDebug()<<sql;
//4、实例化sql语句执行者
QSqlQuery querry;
//5、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this, "提示", "添加信息失败");
return;
}else
{
QMessageBox::information(this, "提示", "添加成功");
}
}
void Widget::on_deleteBtn_clicked()
{
QSqlQuery querry;//实例化一个sql语句执行者
QString sql=QString("DELETE FROM STU ");
int ui_numb = ui->numEdit->text().toUInt(); //获取学号
QString ui_name = ui->nameEdit->text(); //姓名
QString ui_sex = ui->sexEdit->text(); //性别
double ui_score = ui->scoreEdit->text().toDouble();
if(!querry.exec(sql))
{
QMessageBox::information(this, "提示", "删除信息失败");
}else
{
QMessageBox::information(this, "提示", "删除成功");
}
}
//展示信息按钮对应的槽函数
void Widget::on_showBtn_clicked()
{
//1、实例化一个sql语句执行者
QSqlQuery querry;
//2、准备sql语句
QString sql = "select * from STU;";
//3、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","查询失败");
return;
}
//程序执行至此,表示已经完成数据库的查询,并将结果存放到querry对象中,可以通过next函数进行遍历每个结果集
//函数原型:bool next();
//功能:读取查询结果集的下一条记录,如果记录不为空,就返回真,否则返回假
int i = 0; //遍历结果集的行
while (querry.next())
{
//获取当前记录
QSqlRecord record = querry.record();
//对当前记录进行操作,遍历当前记录的所有字段
for(int j=0; j<record.count(); j++)
{
//该循环中的value(j),表示的就是第i行j列的那个元素
//record.value(j).toString();
//ui->msgTable->setItem(i,j, new QTableWidgetItem(record.value(j).toString()));
QTableWidgetItem *item = new QTableWidgetItem(record.value(j).toString()); //将数据库中的字段,封装成ui界面上的一条信息
ui->msgTable->setItem(i,j, item); //将信息进行展示
}
i++; //表示行数增加
}
}