QT day6

本文详细描述了一个使用Qt开发的学生管理系统,涉及数据库连接、表创建、数据添加、查询、修改和删除操作,以及相应的错误处理机制。
摘要由CSDN通过智能技术生成

目录

思维导图

学生管理系统


思维导图

学生管理系统

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;
    }
}

  • 33
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值