案例需求:
完成数据库插入,删除,修改,查看操作。
分为 插入,删除,修改,查看,查询 几个模块。
代码:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
#include <QDateTime>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
void display();
private slots:
void on_insert_button_clicked();
void on_delete_button_clicked();
void on_update_button_clicked();
void on_query_button_clicked();
private:
Ui::Widget *ui;
QSqlDatabase db;
QSqlQuery *query;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("数据库可视化窗口");
// 加载数据库的驱动
db = QSqlDatabase::addDatabase("QSQLITE");
// 设置本地数据库文件
db.setDatabaseName("stu.db");
// 打开数据库
if(db.open()) qDebug()<<"打开成功....";
else qDebug()<<"打开失败....";
// 向数据库发送sql语句
query = new QSqlQuery; // 不可指定父对象
if(query->exec("CREATE TABLE IF NOT EXISTS stu_info (id INT PRIMARY KEY,name STRING(32),score DOUBLE)"))
qDebug()<<"创建table成功....";
else qDebug()<<"创建table失败:"<<query->lastError().text();
// 显示
display();
}
Widget::~Widget()
{
delete ui;
delete query; // 因为调用时不能指定父对象,所以需要手动释放
// 关闭数据库
db.close();
}
// displlay
void Widget::display(){
query->exec(QString("SELECT * FROM stu_info"));
while(query->next()){
int id = query->value("id").toInt();
QString name = query->value("name").toString();
double score = query->value("score").toDouble();
ui->plainTextEdit->appendPlainText(QString("id : %1 | name : %2 | score : %3").arg(id).arg(name).arg(score));
}
}
// insert
void Widget::on_insert_button_clicked(){
QDateTime currentDate = QDateTime::currentDateTime();
QString dateTimeString = currentDate.toString("yyyy-MM-dd hh:mm:ss");
if(ui->num_edit->text() == "" || ui->name_edit->text() == "" || ui->score_edit->text() == ""){
qDebug()<<"信息缺失 插入失败:"<<query->lastError().text();
ui->plainTextEdit->appendPlainText(QString("%1 信息缺失 插入失败....").arg(dateTimeString));
return ;
}
int num = ui->num_edit->text().toInt();
QString name = ui->name_edit->text();
double score = ui->score_edit->text().toDouble();
if(query->exec(QString("INSERT INTO stu_info VALUES('%1','%2','%3')").arg(num).arg(name).arg(score))){
qDebug()<<dateTimeString<<"插入成功....";
ui->plainTextEdit->appendPlainText(QString("%1 插入成功....").arg(dateTimeString));
display();
}
else{
qDebug()<<"插入失败:"<<query->lastError().text();
ui->plainTextEdit->appendPlainText(QString("%1 插入失败....").arg(dateTimeString));
}
}
// delete
void Widget::on_delete_button_clicked()
{
QDateTime currentDate = QDateTime::currentDateTime();
QString dateTimeString = currentDate.toString("yyyy-MM-dd hh:mm:ss");
if(ui->num_edit->text() == ""){
qDebug()<<dateTimeString<<"学号不存在 删除失败....";
ui->plainTextEdit->appendPlainText(QString("%1 学号不存在 删除失败....").arg(dateTimeString));
}
else if(query->exec(QString("DELETE FROM stu_info WHERE id = %1").arg(ui->num_edit->text().toInt()))){
qDebug()<<dateTimeString<<"删除成功....";
ui->plainTextEdit->appendPlainText(QString("%1 删除成功....").arg(dateTimeString));
display();
}
else{
qDebug()<<"删除失败:"<<query->lastError().text();
ui->plainTextEdit->appendPlainText(QString("%1 删除失败....").arg(dateTimeString));
}
}
// update
void Widget::on_update_button_clicked()
{
QDateTime currentDate = QDateTime::currentDateTime();
QString dateTimeString = currentDate.toString("yyyy-MM-dd hh:mm:ss");
if(query->exec(QString("UPDATE stu_info SET id = %1, name = '%2', score = %3 WHERE id = %4").arg(ui->num_edit->text().toInt()).arg(ui->name_edit->text()).arg(ui->score_edit->text().toDouble()).arg(ui->num_edit->text().toInt()))){
qDebug()<<dateTimeString<<"更新成功....";
ui->plainTextEdit->appendPlainText(QString("%1 更新成功....").arg(dateTimeString));
display();
}
else{
qDebug()<<"更新失败:"<<query->lastError().text();
ui->plainTextEdit->appendPlainText(QString("%1 更新失败....").arg(dateTimeString));
}
}
// query
void Widget::on_query_button_clicked()
{//SELECT * FROM stu_info WHERE id = %1
QDateTime currentDate = QDateTime::currentDateTime();
QString dateTimeString = currentDate.toString("yyyy-MM-dd hh:mm:ss");
if(query->exec(QString("SELECT * FROM stu_info WHERE id = %1").arg(ui->num_edit->text().toInt()))){
qDebug()<<dateTimeString<<"查询成功....";
ui->plainTextEdit->appendPlainText(QString("%1 查询成功....").arg(dateTimeString));
// 显示
while(query->next()){
int id = query->value("id").toInt();
QString name = query->value("name").toString();
double score = query->value("score").toDouble();
ui->plainTextEdit->appendPlainText(QString("id : %1 | name : %2 | score : %3").arg(id).arg(name).arg(score));
}
}
else{
qDebug()<<"查询失败:"<<query->lastError().text();
ui->plainTextEdit->appendPlainText(QString("%1 查询失败....").arg(dateTimeString));
}
}
widget.ui
输出:
初始界面:
插入:
修改:
删除:
查询: