pro文件
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += 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.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
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_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_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)
{
}