QT對Mysql的增刪查改

QT對Mysql的增刪查改

都是從B站學的

.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

widget.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

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

    void on_pushButton_Sure_clicked();

    void on_pushButton_Cancle_clicked();

    void on_pushButton_update_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

在這裏插入圖片描述
widget.cpp

#include "widget.h"
#include "ui_widget.h"

#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariantList>

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

    //qDebug()<<QSqlDatabase::drivers();    //查看可用的數據庫驅動

    QSqlDatabase db =  QSqlDatabase::addDatabase("QMYSQL"); // 表示數據庫對象
    // 1.鏈接數據庫------------------------------------------------------------------------------------------
    db.setHostName("127.0.0.1");
    db.setUserName("root");
    db.setPassword("12345678");
    db.setDatabaseName("stu");

    // 2.打開數據庫--------------------------------------------------------------------------------------------
    if(!db.open()){
        QMessageBox::warning(this,"錯誤", db.lastError().text());
        return ;
    }
    else
        qDebug()<<"打開成功!";

    QSqlQuery query;
    // 3.創建一個表-----------------------------------------------------------------------------------------------
    //query.exec("create table student(id int primary key auto_increment,name varchar(255),age int,score int)");

    // 4.1在表中插入一行數據-----------------------------------------------------------------------------------------
    //query.exec("insert into student(id,name,age,score) values(1234,'你大爺',31,62)");

    // 4.2批量插入odbc風格------------------------------------------------------------------------------------------
    /*
    query.prepare("insert into student(name,age,score) values(?,?,?)");
    QVariantList nameList;
    nameList << "香蕉" << "芒果" << "西瓜";
    QVariantList ageList;
    ageList << 21 << 22 << 50;
    QVariantList scoreList;
    scoreList << 59 << 59 << 59;
    query.addBindValue(nameList);
    query.addBindValue(ageList);
    query.addBindValue(scoreList);
    query.execBatch();

    // 4.3批量插入數據oracle風格
    query.prepare("insert into student(name,age,score) values(:name,:age,:score)");
    QVariantList nameList;
    nameList << "朱元璋" << "漢武帝" << "王昭君";
    QVariantList ageList;
    ageList << 320 << 2100 << 500;
    QVariantList scoreList;
    scoreList << 60 << 61 << 58;
    query.bindValue(":name",nameList);
    query.bindValue(":age",ageList);
    query.bindValue(":score",scoreList);
    query.execBatch();
    */

}

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

// 5.刪除一行數據---------------------------------------------------------------------------------------
// 
void Widget::on_pushButton_Del_clicked()
{
    // 獲取行編輯內容
    QString name = ui->lineEdit->text();
    QString sql = QString("delete from student where name = '%1'").arg(name);

    // 開啓一個事務,
    QSqlDatabase::database().transaction();  // 因爲是局部變量可能再重新連接數據庫打開數據庫這麼麻煩的操作,所以QSqlDatabase::database()是直接獲取當前的數據庫
    QSqlQuery query;
    query.exec(sql);
}

void Widget::on_pushButton_Sure_clicked()
{
    // 確認刪除
    QSqlDatabase::database().commit();
}

void Widget::on_pushButton_Cancle_clicked()
{
    // 回滾,撤銷
    QSqlDatabase::database().rollback();
}

// 6.修改一條數據---------------------------------------------------------------------------------------------------------
void Widget::on_pushButton_update_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString score = ui->lineEdit_score->text();
    QString sql = QString("update student set score = '%1' where name = '%2'").arg(score).arg(name);
    QSqlDatabase::database().exec(sql);

}

明天學習查詢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章