C++DAY50

源文件代码

cpp 复制代码
#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");

        db.setDatabaseName("stuinfo.db");
        QMessageBox::information(this,"","创建数据库成功");
    }

    if(!db.open())
    {
        QMessageBox::information(this,"","打开数据库失败");
        return;
    }

    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)";

    if(query.exec(sql))
    {
        QMessageBox::information(this,"","创建数据表成功");
    }
    else
    {
        QMessageBox::information(this,"","创建数据表失败");
    }

}

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


void Widget::on_addbtn_clicked()
{
    int numb = ui->numEdit->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;
    }

    QSqlQuery query;
    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);
    if(query.exec(sql))
    {
        QMessageBox::information(this,"","添加成功");
    }
    else
    {
        QMessageBox::information(this,"","添加失败");
    }
}

void Widget::on_showbtn_clicked()
{
    QSqlQuery query;
    QString sql = "select * from stu_info_table";
    ui->tableWidget->clear();
    if(query.exec(sql))
    {
        int i = 0;
        while(query.next())
        {
            for(int j = 0;j < query.record().count();j++)
            {
                ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));

            }
                i++;
        }
    }
}

void Widget::on_deletebtn_clicked()
{
    QString name = ui->nameedit->text();

    QSqlQuery query;
    QString sql = QString("delete from stu_info_table where name='%1'").arg(name);


    if(query.exec(sql))
    {
        QMessageBox::information(this,"","删除成功");
    }
    else
    {
        QMessageBox::information(this,"","删除失败");
    }
}

void Widget::on_searchbtn_clicked()
{

    QString name = ui->nameedit->text();
    ui->tableWidget->clear();

    QSqlQuery query;
    QString sql = QString("select * from stu_info_table where name = '%1' ").arg(name);

    if(query.exec(sql))
    {
        QMessageBox::information(this, "", "查找成功");
        int i = 0;
        while(query.next())
        {
            for (int j = 0; j < query.record().count(); j++)
            {
                ui->tableWidget->setItem(i, j, new QTableWidgetItem(query.value(j+1).toString()));
            }
            i++;
        }
    }
    else
        QMessageBox::information(this, "", "查找失败");

}
相关推荐
专注仿真2 分钟前
决策树的算法
开发语言·高性能算法
Evand J4 分钟前
【电机滤波例程2】负载转矩增广扩展卡尔曼滤波(EKF)原理与MATLAB例程:五维PMSM状态与负载阶跃估计。订阅专栏后可查看完整代码
开发语言·matlab·电机·ekf·卡尔曼滤波
清水白石00811 分钟前
Python 对象模型深度解析:从“一切皆对象”到 id、type、isinstance 底层机制与小整数缓存原理
开发语言·python·缓存
燕落南枝16 分钟前
c语言 洛谷P1765手机题解
c语言·开发语言
Rosanci18 分钟前
Codex 下载与本地部署实战:从安装到运行全流程指南
开发语言·前端·算法·chatgpt·codex
zhangzeyuaaa23 分钟前
深入 Ruby:Block、Proc、Lambda 核心区别与最佳实践
开发语言·前端·ruby
蜗牛互联网26 分钟前
Java 17调用Embeddings API:余弦相似度与FAQ拒答阈值
java·开发语言·人工智能
旖旎夜光29 分钟前
LeetCode 1576: 替换所有的问号(模拟) —— 题解
c++·学习·算法·leetcode·力控
郝学胜-神的一滴30 分钟前
AI 编程智能体 05:拆解智能体分级体系、类型与全行业落地场景
开发语言·人工智能·python·程序人生·pycharm
朝朝辞暮i30 分钟前
C++ 第 38 课:线程、SingleThreadedExecutor、MultiThreadedExecutor、Callback Group
开发语言·c++·算法·ros2