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 

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

QT对Mysql的增删查改_第1张图片
widget.cpp

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

#include 
#include 
#include 
#include 
#include 
#include 

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

    //qDebug()<

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

}

明天学习查询。

你可能感兴趣的:(Qt基础控件学习)