当前位置: 首页 > article >正文

QTday4

数据库头文件 

#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include<QSqlDatabase>                //数据库管理类
#include<QSqlQuery>                    //数据库查询类
#include<QSqlRecord>                   //记录类


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

    void on_showBtn_clicked();


private:
    Ui::Widget *ui;


    //实例化一个数据库对象
    QSqlDatabase db;
};
#endif // WIDGET_H

数据库源文件

#include "widget.h"
#include "ui_widget.h"
#include<QMessageBox>             //消息对话框
#include<QDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    //想要添加某个数据库
    if(!db.contains("stu.db"))
    {
        //如果当前对象中没有包含所需的数据库,则添加一个数据库
        db = QSqlDatabase::addDatabase("QSQLITE");              //添加一个sqlite3的数据库
        db.setDatabaseName("stu.db");                         //设置数据库的名称
    }


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


    //程序执行至此,表示数据库创建成功并打开了
    //准备创建数据表
    //1、实例化一个sql语句的执行者
    QSqlQuery querry;
    //2、准备sql语句
    QString sql = "create table if not exists STU(id int, name char, sex char, score double);";
    //3、执行sql语句
    if(!querry.exec(sql))
    {
        QMessageBox::information(this, "提示", "数据表创建失败");
        return;
    }


    //程序执行至此,表示数据表也创建成功了


}


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


//添加信息按钮对应的槽函数
void Widget::on_addBtn_clicked()
{
    //1、将ui界面上的相关信息获取下来
    int ui_numb = ui->numEdit->text().toUInt();           //获取学号
    QString ui_name = ui->nameEdit->text();                //姓名
    QString ui_sex = ui->sexEdit->text();                //性别
    double ui_score = ui->scoreEdit->text().toDouble();      //分数


    //2、判断信息中是否有空的
    if(ui_numb==0 || ui_name.isEmpty() || ui_sex.isEmpty() || ui_score==0)
    {
        QMessageBox::information(this,"提示","请将信息填写完整");
        return;
    }


    //3、准备sql语句
    QString sql = QString("insert into STU(id, name, sex, score) values(%1, '%2', '%3', %4);")
                             .arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);
    //qDebug()<<sql;


    //4、实例化sql语句执行者
    QSqlQuery querry;
    //5、执行sql语句
    if(!querry.exec(sql))
    {
        QMessageBox::information(this, "提示", "添加信息失败");
        return;
    }else
    {
        QMessageBox::information(this, "提示", "添加成功");
    }
}
void Widget::on_deleteBtn_clicked()
{
    QSqlQuery querry;//实例化一个sql语句执行者
    QString sql=QString("DELETE FROM STU ");
    int ui_numb = ui->numEdit->text().toUInt();           //获取学号
    QString ui_name = ui->nameEdit->text();                //姓名
    QString ui_sex = ui->sexEdit->text();                //性别
    double ui_score = ui->scoreEdit->text().toDouble();
    
    if(!querry.exec(sql))
    {
        QMessageBox::information(this, "提示", "删除信息失败");
    }else
    {
        QMessageBox::information(this, "提示", "删除成功");
    }
}

//展示信息按钮对应的槽函数
void Widget::on_showBtn_clicked()
{
    //1、实例化一个sql语句执行者
    QSqlQuery querry;


    //2、准备sql语句
    QString sql = "select * from STU;";


    //3、执行sql语句
    if(!querry.exec(sql))
    {
        QMessageBox::information(this,"提示","查询失败");
        return;
    }


    //程序执行至此,表示已经完成数据库的查询,并将结果存放到querry对象中,可以通过next函数进行遍历每个结果集
    //函数原型:bool next();
    //功能:读取查询结果集的下一条记录,如果记录不为空,就返回真,否则返回假
    int i = 0;                  //遍历结果集的行
    while (querry.next())
    {
        //获取当前记录
        QSqlRecord record = querry.record();


        //对当前记录进行操作,遍历当前记录的所有字段
        for(int j=0; j<record.count(); j++)
        {
            //该循环中的value(j),表示的就是第i行j列的那个元素
            //record.value(j).toString();
            //ui->msgTable->setItem(i,j, new QTableWidgetItem(record.value(j).toString()));
            QTableWidgetItem *item = new QTableWidgetItem(record.value(j).toString());    //将数据库中的字段,封装成ui界面上的一条信息
            ui->msgTable->setItem(i,j, item);            //将信息进行展示
        }


        i++;             //表示行数增加
    }
}

 


http://www.kler.cn/news/343355.html

相关文章:

  • 快来了解 Java 内存数据库 H2,不要错过哦
  • 免杀对抗—javaASMMSF源码特征修改汇编调用CS内联C
  • Qt源码-Qt多媒体音频框架
  • 《Image Processing GNN: Breaking Rigidity in Super-Resolution》CVPR2024
  • 鸿蒙OS投票机制
  • 『网络游戏』进入游戏主城UI跳转主城【26】
  • 动态规划-路径问题——931.下降路径最小和
  • AI测试入门:认识Graph RAG
  • 京东零售数据湖应用与实践
  • Graphics2D 打包在Linux运行时中文乱码,展示成方格
  • JavaScript轮播图实现
  • 两个数相加(c语言)
  • MySQL修改字段卡住问题总结及解决方法
  • hive数据库||的用法、hive数据库字符串拼接、concat函数、concat_ws函数
  • Patroni配置3-环境变量配置设置
  • 【操作系统】四、文件管理:1.文件系统基础(文件属性、文件逻辑结构、文件物理结构、文件存储管理、文件目录、基本操作、文件共享、文件保护)
  • QT C++ 软键盘/悬浮键盘/触摸屏键盘的制作
  • leetcode:反转字符串中的单词III
  • 2024年秋季学期期中考试成绩查询系统-老师制作工具
  • ECharts图表图例5