SQL 操作。

數(shù)據(jù)庫QMYSQL

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlTableModel>   // 數(shù)據(jù)模型
// 顯示模型中的數(shù)據(jù), 需要使用視圖 QTableView - QSqlTableModel
// qt model - view 模型

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_submit_clicked();
    void on_revert_clicked();
    void on_search_clicked();
private:
    Ui::MainWindow *ui;
    QSqlTableModel* model;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //添加sql數(shù)據(jù)庫
    // 1.添加一個(gè)mysql數(shù)據(jù)庫, pc端,需要安裝mysql數(shù)據(jù)庫,用于讓qt連接。
    qDebug() << QSqlDatabase::drivers();
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    //  2.設(shè)置數(shù)據(jù)庫連接信息
    db.setHostName("127.0.0.1"); // 主機(jī)IP
    db.setUserName("root"); // 登錄mysql數(shù)據(jù)庫的用戶名
    db.setPassword("root"); // 登錄密碼
    db.setDatabaseName("itcast"); // 連接的數(shù)據(jù)庫名
    if(db.open() == false) // 3.打開數(shù)據(jù)庫
    {
        QMessageBox::warning(this, "warning", db.lastError().text());
    }

    // 增刪查改 ...
    // 添加一條記錄
    QSqlQuery query;
    //    QString sql = "insert into people(name, age) values('張三豐', 123)";
    //    query.exec(sql);
    // 預(yù)處理
    // ? -- 通配符, odbc風(fēng)格的通配符
    //    query.prepare("insert into people(name, age) values(?, ?)");
    //    // 添加綁定數(shù)據(jù),批處理
    //    QVariantList nameList;
    //    nameList << "aa" << "bb" << "cc";
    //    query.addBindValue(nameList);
    //    QVariantList ageList;
    //    ageList << 12 << 13 << 14;
    //    query.addBindValue(ageList);
    //    // 執(zhí)行批處理
    //    query.execBatch();

    // oracle 風(fēng)格的通配符
    // 定義方式:          :+自定義的名字
    //    query.prepare("insert into people(name, age) values(:name, :age)");
    //    // 添加綁定數(shù)據(jù)
    //    QVariantList ageList;
    //    ageList << 12 << 13 << 14;
    //    query.bindValue(":age", ageList);
    //    QVariantList nameList;
    //    nameList << "aa11" << "bb222" << "cc333";
    //    query.bindValue(":name", nameList);
    //    // 執(zhí)行批處理
    //    query.execBatch();

    // 數(shù)據(jù)查詢******
    query.exec("select name, age from people");
    while(query.next()) // 遍歷每一條記錄
    {
        qDebug() << query.value(0).toString().toUtf8().data() // 0 -- 第一個(gè)字段的索引  0/name.
                 << query.value("name").toString().toUtf8().data(); // 1/name
    }

    // 1. 實(shí)例化model
    model = new QSqlTableModel(this);
    // 2. 將模型設(shè)置到視圖tableView中
    ui->tableView->setModel(model);
    // 3. 給model設(shè)置數(shù)據(jù)庫表 -- 前提條件: 數(shù)據(jù)庫已經(jīng)打開了
    model->setTable("student");
    // 4. 查詢表
    model->select();
    // 5. 設(shè)置水平表頭
    model->setHeaderData(0, Qt::Horizontal, "編號");
    // 6. 設(shè)置提交模式,不自動提交
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_submit_clicked() //提交
{
    qDebug()  << "on_submit_clicked";
    model->submitAll(); //槽函數(shù)
}
void MainWindow::on_revert_clicked() //撤銷
{
    qDebug()  << "on_revert_clicked";
    model->revertAll(); // 撤銷步驟
    model->submitAll(); // 提交步驟 -- 更新數(shù)據(jù)模型 model->select();
}
void MainWindow::on_search_clicked()//query
{
    qDebug()  << "on_search_clicked";
    QString name = ui->lineEdit->text();    // 查詢的人的名字
    //slect * from aa  where name = 'xiaoming';
    // 設(shè)置過濾條件 name. 查詢字符串,要帶單引號
    QString sql = QString("name='%1'").arg(name);
    model->setFilter(sql);
    // model 重新查詢
    model->select();
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 創(chuàng)建一個(gè)線程,命名為ThreadOne,啟動線程,要求能夠打印1~10的所有數(shù)字,每隔1秒打印。 mainwind...
    Aya呸閱讀 226評論 0 0
  • spark 2.X與1.x的區(qū)別 spark sql 2.x以上版本和1.x版本有個(gè)很大的區(qū)別:spark1.x的...
    jackLee閱讀 3,129評論 0 3
  • 在編寫shell腳本的時(shí)候,可能會遇到操作mysql數(shù)據(jù)庫的情況。下面介紹如何在shell腳本中操作mysql數(shù)據(jù)...
    ifcoder閱讀 2,746評論 0 2
  • 安裝Qt及Qt Creator 在Terminal中輸入 其中 qt4-dev-tools中包含了Qt Assis...
    帥碧閱讀 389評論 0 2
  • 前面寫了如何使用 SDL 顯示一張 BMP 圖片,但這并不是我們最終的目的。我們最終的目的是使用 SDL 顯示 Y...
    村口大白楊閱讀 1,917評論 0 2

友情鏈接更多精彩內(nèi)容