widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>//数据库管理类
#include<QSqlQuery>//执行sql语句类
#include<QSqlRecord>//数据库记录类
#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_delBtn_clicked();
//修改按钮
void on_upBtn_clicked();
private:
Ui::Widget *ui;
//实例化一个数据库对象
QSqlDatabase db;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#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.db");
}
//打开数据库,创建数据表
if(!db.open())
{
//说明数据库打开失败
QMessageBox::information(this,"","打开数据库失败");
return;
}
//程序运行到此,说明数据库创建成功,并且打开
//创建数据表
//实例化一个执行sql语句的对象
QSqlQuery query;
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,"","数据库表创建失败");
return;
}
}
Widget::~Widget()
{
delete ui;
}
//添加按钮对应的槽函数处理
void Widget::on_addBtn_clicked()
{
//获取ui界面上的信息
int numb=ui->numEdit->text().toInt();
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语句
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语句的对象
QSqlQuery query;
//执行sql语句
if(query.exec(sql))
{
ui->numEdit->clear();
ui->nameEdit->clear();
ui->sexEdit->clear();
ui->scoreEdit->clear();
QMessageBox::information(this,"","添加成功");
}
else
{
QMessageBox::information(this,"","添加失败");
return;
}
}
//显示按钮对应的槽函数处理
void Widget::on_showBtn_clicked()
{
//准备sql语句
QString sql="select * from stu_info_table";
//实例化一个执行sql语句的对象
QSqlQuery query;
//执行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++)
{
//将第i行第j列的元素放入ui界面上
ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));
}
i++;
}
}
//删除对应的槽函数处理
void Widget::on_delBtn_clicked()
{
//根据学号删除
QString numb=ui->numEdit->text();
//实例化一个执行sql语句的对象
QSqlQuery query;
query.exec(QString("select * from stu_info_table where numb='%1'").arg(numb));
if(query.next())
{
//准备sql语句
QString sql=(QString("delete from stu_info_table where numb='%1'").arg(numb));
if(query.exec(sql))
{
ui->numEdit->clear();
QMessageBox::information(this,"","删除成功");
}
}
else
{
QMessageBox::information(this,"","你输入的学号不正确,请重新输入");
}
}
//修改对应的槽函数处理
void Widget::on_upBtn_clicked()
{
//根据学号修改
QString numb=ui->numEdit->text();
QString name=ui->nameEdit->text();
QString sex=ui->sexEdit->text();
QString score=ui->scoreEdit->text();
//保证信息填写完整
if(numb==0||name.isEmpty()||sex.isEmpty()||score==0)
{
QMessageBox::information(this,"","请将信息填写完整");
return;
}
//实例化一个执行sql语句的对象
QSqlQuery query;
query.exec(QString("select * from stu_info_table where numb='%1'").arg(numb));
if(query.next())
{
ui->numEdit->clear();
ui->nameEdit->clear();
ui->sexEdit->clear();
ui->scoreEdit->clear();
//准备sql语句
QString sql=(QString("update stu_info_table set name='%1',sex='%2',score='%3' where numb='%4'").arg(name).arg(sex).arg(score).arg(numb));
if(query.exec(sql))
{
QMessageBox::information(this,"","修改成功");
}
}
else
{
QMessageBox::information(this,"","你输入的学号不正确,请重新输入");
}
}