2023.10.23

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include //数据库管理类
#include //执行SQL语句
#include //数据库记录类
#include //数据库错误类
#include 


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_dele_Btn_clicked();

    void on_find_Btn_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())
    {
        //说明不存在
        //创建数据库
        db = QSqlDatabase::addDatabase("QSQLITE");//表示数据库驱动sqlite3
        //给数据库命名
        db.setDatabaseName("stuInfo.db");
        QMessageBox::information(this,"创建成功","创建成功");

    }

    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"失败","失败");
                return;
    }
    //创建数据库表
    //实例化sql的执行语句的类对象
    QSqlQuery qury;
    //准备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(qury.exec(sql))
    {
        QMessageBox::information(this,"成功","1111111");
    }
    else
    {
        QMessageBox::information(this,"失败","0000000");
    }

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_AddBtn_clicked()
{
    int id = ui->idEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit_3->text();
    int score = ui->soreEdit_4->text().toUInt();
    //保证输入完整
    if(id==0 || name.isEmpty() || sex.isEmpty() || score==0)
    {
        QMessageBox::information(this,"","信息不完整");
        return;
    }
    //写入数据
    QSqlQuery qury;
    QString sql = QString("insert into stu_info_table(numb,name,sex,score) "
                          "values(%1,'%2','%3',%4)")
                            .arg(id).arg(name).arg(sex).arg(score);
    //执行sql
    if(qury.exec(sql))
    {
        QMessageBox::information(this,"成功","22222");
    }
    else
    {
        QMessageBox::information(this,"失败","xxxxxx");
    }
}

void Widget::on_ShowBtn_clicked()
{
    //实例化执行sql的类对象
    QSqlQuery qury;
    //准备sql语句
    QString sql = "select * from stu_info_table";
    //执行sql
    if(qury.exec(sql))
    {
        //记录行号
        int i = 0;
        //将数据库内的内容放入ui界面上
        while (qury.next())//遍历内容
        {
            for (int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(qury.value(j+1).toString()));
            }
            i++;
        }
    }

}

void Widget::on_dele_Btn_clicked()
{
    ui->tableWidget->clear();
    QSqlQuery qury;
    //实例化执行sql的类对象
    QString s1;
    int s2;
    int id = ui->idEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit_3->text();
    int score = ui->soreEdit_4->text().toUInt();
    if(!name.isEmpty())
    {
        s1=name;
        QString sql = QString("delete from stu_info_table where name='%1';").arg(s1);
        qury.exec(sql);
    }
    else if(id!=0)
    {
        s2=id;
        QString sql = QString("delete from stu_info_table where id=%1;").arg(s2);
        qury.exec(sql);
    }
    else if(!sex.isEmpty())
    {
        s1=sex;
        QString sql = QString("delete from stu_info_table where sex='%1';").arg(s1);
        qury.exec(sql);
    }
    else if(score!=0)
    {
        s2=score;
        QString sql = QString("delete from stu_info_table where score=%1;").arg(s2);
        qury.exec(sql);
    }
}

void Widget::on_find_Btn_clicked()
{
    QSqlQuery qury;
    QString s1;
    int s2;
    int id = ui->idEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit_3->text();
    int score = ui->soreEdit_4->text().toUInt();
    ui->tableWidget->clear();
    if(!name.isEmpty())
    {
        s1=name;
        QString sql=QString("select numb, name, sex, score from stu_info_table where name='%1'").arg(s1);
        qury.exec(sql);
        //记录行号
        int i = 0;
        //将数据库内的内容放入ui界面上
        while (qury.next())//遍历内容
        {
            for (int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(qury.value(j).toString()));
            }
            i++;
        }
    }
    else if(id!=0)
    {
        s2=id;
        QString sql=QString("select numb, name, sex, score from stu_info_table where id=%1").arg(s2);
        qury.exec(sql);
        //记录行号
        int i = 0;
        //将数据库内的内容放入ui界面上
        while (qury.next())//遍历内容
        {
            for (int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(qury.value(j).toString()));
            }
            i++;
        }
    }
    else if(!sex.isEmpty())
    {
        s1=sex;
        QString sql=QString("select numb, name, sex, score from stu_info_table where sex='%1'").arg(s1);
        qury.exec(sql);
        //记录行号
        int i = 0;
        //将数据库内的内容放入ui界面上
        while (qury.next())//遍历内容
        {
            for (int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(qury.value(j).toString()));
            }
            i++;
        }
    }
    else if(score!=0)
    {
        s2=score;
        QString sql=QString("select numb, name, sex, score from stu_info_table where score=%1").arg(s2);
        qury.exec(sql);
        //记录行号
        int i = 0;
        //将数据库内的内容放入ui界面上
        while (qury.next())//遍历内容
        {
            for (int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(qury.value(j).toString()));
            }
            i++;
        }
    }

}

你可能感兴趣的:(c++)