目录
ui界面
头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase> //数据库管理类
#include <QSqlQuery> //执行sql语句类
#include <QSqlRecord> //数据库记录类
#include <QSqlError> //数据库错误类
#include <QMessageBox> //消息对话框类
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_showBtn_clicked();
void on_updateBtn_clicked();
void on_deleteBtn_clicked();
private:
Ui::Widget *ui;
QSqlDatabase db; //实例化一个数据库
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//判断是否有该数据库
if(!db.contains("stuInfo.db"))
{
//说明数据库不存在,需要创建数据库
db = QSqlDatabase::addDatabase("QSQLITE"); //驱动为sqlite3
//给刚才创建的数据库设置数据库名
db.setDatabaseName("stuInfo");
}
//打开数据库
if(!db.open())
{
QMessageBox::information(this,"","打开数据库失败!");
return;
}
//创建数据库表
//实例化一个执行sql语句的对象
QSqlQuery query;
//准备sql语句
QString sql = "create table if not exists stu_info_table("
"id integer primary key autoincrement,"
"numb integer,"
"name varchar(20),"
"sex varchar(4),"
"score integer)";
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","创建数据库表成功!");
}
else
{
QMessageBox::information(this,"","创建数据库表失败!");
}
}
Widget::~Widget()
{
delete ui;
}
//添加按钮对应的槽函数处理
void Widget::on_addbtn_clicked()
{
//获取ui界面上的学生信息
int numb = ui->numberEdit->text().toUInt();
QString name = ui->nameEdit->text();
QString sex = ui->sexEdit->text();
int score = ui->scoreEdit->text().toUInt();
//判断用户是否填写完整信息
if(numb==0 || name.isEmpty() || sex.isEmpty() || score==0)
{
QMessageBox::information(this,"","请将信息填写完整!");
return;
}
//实例化一个执行sql语句的对象
QSqlQuery query;
//准备sql语句
QString sql = QString("insert into stu_info_table(numb,name,sex,score)"
"values(%1,'%2','%3',%4)").arg(numb).arg(name).arg(sex).arg(score);
//执行sql语句
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","添加成功!");
}
else
{
QMessageBox::information(this,"","添加失败!");
}
}
//显示按钮对应的槽函数处理
void Widget::on_showBtn_clicked()
{
ui->tableWidget->clear();
//实例化一个执行sql语句的对象
QSqlQuery query;
//准备sql语句
QString sql = "select * from Stu_info_table";
//执行sql语句
if(!query.exec(sql))
{
QMessageBox::information(this,"","查询失败!");
return;
}
//所查询的信息就已经存放到query对象中
int i=0; //记录行号
while (query.next())
{
for(int j=0;j<query.record().count();j++)
{
//将数据放到ui界面上
ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));
}
i++; //行数递增
}
}
//修改按钮对应的槽函数处理
void Widget::on_updateBtn_clicked()
{
//实例化一个执行sql语句的对象
QSqlQuery query;
int numb = ui->numberEdit->text().toUInt();
QString name = ui->nameEdit->text();
QString sex = ui->sexEdit->text();
int score = ui->scoreEdit->text().toUInt();
if(numb!=0)
{
if(!name.isEmpty())
{
//准备sql语句
QString sql = QString("update Stu_info_table set name='%1' where numb=%2").arg(name).arg(numb);
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","姓名修改成功!");
}
else
{
QMessageBox::information(this,"","姓名修改失败!");
return;
}
}
if(!sex.isEmpty())
{
//准备sql语句
QString sql = QString("update Stu_info_table set sex='%1' where numb=%2").arg(sex).arg(numb);
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","性别修改成功!");
}
else
{
QMessageBox::information(this,"","性别修改失败!");
return;
}
}
if(score!=0)
{
//准备sql语句
QString sql = QString("update Stu_info_table set score=%1 where numb=%2").arg(score).arg(numb);
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","分数修改成功!");
}
else
{
QMessageBox::information(this,"","分数修改失败!");
return;
}
}
if(name.isEmpty() && sex.isEmpty() && score==0)
{
QMessageBox::information(this,"","请输入要修改的内容!");
return;
}
}else
{
QMessageBox::information(this,"","请输入要修改的学生学号!");
}
}
//删除按钮对应的槽函数处理
void Widget::on_deleteBtn_clicked()
{
//实例化一个执行sql语句的对象
QSqlQuery query;
int numb = ui->numberEdit->text().toUInt();
//准备sql语句
QString sql = QString("delete from Stu_info_table where numb=%1").arg(numb);
//执行sql语句
if(query.exec(sql))
{
QMessageBox::information(this,"","删除成功!");
}
else
{
QMessageBox::information(this,"","删除失败!");
return;
}
}