#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_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_addBtn_clicked();void on_showBtn_clicked();void on_delBtn_clicked();void on_updateBtn_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("stdInfo");}//打开数据库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);//arg()的返回值是自身的引用,所以可以接着用//执行sql语句if(query.exec(sql)){QMessageBox::information(this,"","添加成功!");}else{QMessageBox::information(this,"","添加失败!");}
}
//显示按钮对应的槽函数处理
void Widget::on_showBtn_clicked()
{ui->tableWidget->clear();//准备执行sql的对象QSqlQuery query;QString sql="select *from stu_info_table";//如果查询失败if(!query.exec(sql)){QMessageBox::information(this,"","查询失败!");return;}//查询成功,所查询的信息就已经存放到query中了//遍历int i=0;//记录行int j=0;//记录列while(query.next()){for(j=0;j<query.record().count();j++)//遍历列{//将数据放到ui界面上ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));}i++;//行数递增}
}
//删除按钮对应的槽函数处理
void Widget::on_delBtn_clicked()
{//准备执行sql语句的对象QSqlQuery query;int numb=ui->numberEdit->text().toUInt();QString sql=QString("delete from stu_info_table where numb=%1 ").arg(numb);//判断删除成功失败if(!query.exec(sql)){QMessageBox::information(this,"","删除失败!");return;}}
//修改按钮对应的槽函数处理
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||name.isEmpty()){//如果信息不完整,提示,并退出QMessageBox::information(this,"","请填入修改的信息");return;}QString sql=QString("update stu_info_table set name='%1' where numb=%2").arg(name).arg(numb);if(!query.exec(sql)){QMessageBox::information(this,"","修改失败!");return;}}
0115qt - 幕布