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

qt数据库的系统

pro文件

QT       += core gui sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.hFORMS += \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

头文件

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<qsqldatabase.h> //数据库管理类
#include<QSqlQuery>
#include<qsqlrecord.h>//记录类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_delebtn_clicked();void on_sortbtn_clicked();void on_sortComboBox_activated(const QString &arg1);private:Ui::Widget *ui;//实例化一个数据库对象QSqlDatabase db;
};
#endif // WIDGET_H

源文件

#include "widget.h"
#include "ui_widget.h"
#include<qmessagebox.h>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;}QSqlQuery querry;QString sql="create table if not exists STU(id int,name char,sex char, score double);";if(!querry.exec(sql)){QMessageBox::information(this,"提示","数据表打开失败");return;}}Widget::~Widget()
{delete ui;
}//点击添加信息按钮的槽函数
void Widget::on_addbtn_clicked()
{//1.将ui界面上的信息获取下来int ui_numb=ui->idedit->text().toUInt();QString ui_name=ui->nameedit->text();QString ui_sex=ui->sexedit->text();double ui_score=ui->scoreedit->text().toDouble();if(ui_numb==0||ui_sex.isEmpty()||ui_sex.isEmpty()||ui_score==0){QMessageBox::information(this,"提示","将信息填完整");return;}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);QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"提示","添加信息失败");}else{ui->nameedit->clear();ui->sexedit->clear();ui->idedit->clear();ui->scoreedit->clear();QMessageBox::information(this,"提示","添加信息成功");}}void Widget::on_showbtn_clicked()
{//1.先实例化一个sql语句执行者QSqlQuery querry;//2.准备sql语句QString sql="select *from STU";if(!querry.exec(sql)){QMessageBox::information(this,"提示","查询失败");return;}ui->msgtable->clear();int i=0;while(querry.next()){QSqlRecord record=querry.record();for(int j=0;j<record.count();j++){ui->msgtable->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));}i++;}}void Widget::on_delebtn_clicked()
{int ui_numb = ui->idedit->text().toUInt();QString ui_name=ui->nameedit->text();QString ui_sex=ui->sexedit->text();double ui_score=ui->scoreedit->text().toDouble();if (ui_numb == 0 && ui_name.isEmpty()&&ui_sex.isEmpty()&&ui_score==0){QMessageBox::information(this, "提示", "请输入有效的ID或姓名或性别或成绩");return;}if (ui_numb != 0){QString sql = QString("DELETE FROM STU WHERE id = %1;").arg(ui_numb);QSqlQuery query;if (!query.exec(sql)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}// 准备删除SQL语句if (!ui_name.isEmpty()){QString sql1 = QString("DELETE FROM STU WHERE name = '%2';").arg(ui_name);QSqlQuery query1;if (!query1.exec(sql1)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}if (ui_score != 0){QString sql2 = QString("DELETE FROM STU WHERE score = %3;").arg(ui_score);QSqlQuery query2;if (!query2.exec(sql2)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}if (!ui_sex.isEmpty()){QString sql3 = QString("DELETE FROM STU WHERE sex = '%2';").arg(ui_sex);QSqlQuery query3;if (!query3.exec(sql3)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}}void Widget::on_sortbtn_clicked()
{// 1. 获取选中的排序关键字QString sortColumn;if (ui->sortComboBox->currentText() == "ID") {sortColumn = "id";} else if (ui->sortComboBox->currentText() == "姓名") {sortColumn = "name";} else if (ui->sortComboBox->currentText() == "性别") {sortColumn = "sex";} else if (ui->sortComboBox->currentText() == "成绩") {sortColumn = "score";}QString sortOrder = "ASC"; // 可以根据需要修改为 "DESC"// 2. 构建 SQL 查询语句QString sql = QString("SELECT * FROM STU ORDER BY %1 %2;").arg(sortColumn).arg(sortOrder);QSqlQuery query;if (!query.exec(sql)){QMessageBox::information(this, "提示", "排序失败");return;}// 3. 清空表格并显示排序后的结果ui->msgtable->clear();int i = 0;while (query.next()){QSqlRecord record = query.record();for (int j = 0; j < record.count(); j++){ui->msgtable->setItem(i, j, new QTableWidgetItem(record.value(j).toString()));}i++;}}void Widget::on_sortComboBox_activated(const QString &arg1)
{}


http://www.mrgr.cn/news/48381.html

相关文章:

  • dockers网络连接指令:docker network connect
  • 空间异质性数据分析不再复杂:地理加权回归分析、主成分分析、判别分析、分位数回归分析、线性回归等
  • Windows远程桌面连接到Linux
  • 基于Java Springboot宠物咖微信小程序
  • 车辆传动系统的simulink建模与仿真,分析加速和刹车两个工况
  • hhdb数据库介绍(10-33)
  • CANoe_使用C#动态生成控件快速部署程序
  • 【分布式架构】分布式锁Redission
  • NumPy 第十一课 -- 广播(Broadcast)
  • MySQL8.0如何优化网卡MTU值,提高数据库性能?
  • 硬盘数据恢复工具:拯救数据丢失的利器
  • Python | Leetcode Python题解之第474题一和零
  • 数据库SQL基础教程(二)
  • 学习 go 语言开发一个月能赚 10 万?
  • Git 常用的操作命令大全
  • 物理学基础精解【73】
  • 连接达梦数据库的N种方式
  • 48 C 语言实战项目——客户信息管理系统
  • 一天认识一个硬件之散热器
  • 华为启动鸿蒙原生应用开发者激励计划 12月31日前上架最高可获10万元
  • Java | Leetcode Java题解之第474题一和零
  • 你能描述一下Java中的JMX(Java Management Extensions)吗?Java中的JavaFX是什么?它与Swing有何不同?
  • MySQL8.0是如何使用SSL/TLS加密提高数据库的安全性的?
  • Promise.race()
  • 大一计算机课程之线性代数
  • int QSqlQuery::size() const