您好,登錄后才能下訂單哦!
這篇文章主要講解了“Qt怎么連接數據庫并實現數據庫增刪改查”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Qt怎么連接數據庫并實現數據庫增刪改查”吧!
先來看下連接數據庫的效果圖。
“連接數據庫”按鈕的槽函數如下:
void MainWindow::on_pushButton_connectDataBase_clicked() { //連接數據庫 db.setHostName("127.0.0.1"); db.setPort(3306); db.setDatabaseName("test");//數據庫名稱 db.setUserName("root");//用戶名 db.setPassword("root");//密碼 qDebug()<<"available drivers:"; QStringList drivers = QSqlDatabase::drivers(); for(auto driver: drivers) qDebug() << driver; bool ok = db.open(); if(ok) { QMessageBox::information(this, "提示","數據庫連接成功"); qDebug()<<"數據庫連接成功"; } else { QMessageBox::information(this, "提示","數據庫連接失敗"); this->close(); qDebug()<<"數據庫連接失敗"; } //若數據庫中沒有表,則新建 QSqlQuery query; QString createTableUser="create table if not exists user(name varchar(8),age int(255), " "sex varchar(8), number int(255));"; query.exec(createTableUser); }
若在數據庫名等信息沒有填錯的情況下,數據庫連接失敗,并打印以下信息,情參考該鏈接解決:http://www.neiyidaogou.com/article/281192.htm
當運行點擊"連接數據庫"按鈕后,會根據信息連接對應的數據庫,若連接的該數據庫沒有user表時,會自動創建一個user表,創建的表信息如下圖所示,該圖為Navicat Premium 15軟件內的截圖。
先看下效果圖:
首先定義一個QSqlQueryModel數據模型,并于對應的QTableView綁定,如下:
QSqlQueryModel *userMode;//數據模型 userMode = new QSqlQueryModel(ui->tableView);//綁定
設置點擊查詢的槽函數:
//查詢 void MainWindow::on_pushButton_query_clicked() { userMode->setQuery("select * from user;"); ReshowTable(); } void MainWindow::ReshowTable() { userMode->setHeaderData(0,Qt::Horizontal,tr("姓名")); userMode->setHeaderData(1,Qt::Horizontal,tr("年齡")); userMode->setHeaderData(2,Qt::Horizontal,tr("性別")); userMode->setHeaderData(3,Qt::Horizontal,tr("手機號")); ui->tableView->setModel(userMode); }
先看添加的效果圖:
在信息輸入內對應填寫各信息,填寫完成后點擊“添加”按鈕,可添加完成輸入的對應信息,與數據庫內容同步,添加按鈕的槽函數:
//添加 void MainWindow::on_pushButton_add_clicked() { QString name = ui->lineEdit_name->text(); QString age = ui->lineEdit_age->text(); QString sex = ui->lineEdit_sex->text(); QString number = ui->lineEdit_number->text(); QSqlQuery query; QString str1 = "'" + name +"','" + age + "','" +sex+"','"+number+"'"; QString str = "insert into user(name, age, sex, number) values(" + str1 + ");"; query.exec(str); userMode->setQuery("select * from user;"); ReshowTable(); }
修改的效果圖:
設置點擊表格中的某一列,然后將點擊那一列的信息顯示在編輯信息的文本框內,修改完成后點擊修改即可完成修改,對應函數如下函數:
void MainWindow::on_tableView_pressed(const QModelIndex &index) { QString name = userMode->data(userMode->index(index.row(), 0)).toString(); QString age = userMode->data(userMode->index(index.row(), 1)).toString(); QString sex = userMode->data(userMode->index(index.row(), 2)).toString(); QString number = userMode->data(userMode->index(index.row(), 3)).toString(); ui->lineEdit_name->setText(name); ui->lineEdit_age->setText(age); ui->lineEdit_sex->setText(sex); ui->lineEdit_number->setText(number); } //修改 void MainWindow::on_pushButton_change_clicked() { QSqlQuery query; QString str = "update user set name='" + ui->lineEdit_name->text() + "',age='" + ui->lineEdit_age->text() + "',sex='" + ui->lineEdit_sex->text() + "' where number='" + ui->lineEdit_number->text() + "';"; query.exec(str); QMessageBox::information(this, "修改成功", "表信息修改成功"); userMode->setQuery("select * from user;"); ReshowTable(); }
刪除的效果圖:
刪除的槽函數如下:
//刪除 void MainWindow::on_pushButton_delete_clicked() { QSqlQuery query; int currentrow = ui->tableView->currentIndex().row(); userMode->removeRow(currentrow); if(currentrow != -1)//若選中一行 { int ok = QMessageBox::warning(this,tr("刪除當前行!"),tr("你確定刪除當前行嗎?"), QMessageBox::Yes,QMessageBox::No); //如確認刪除 if(ok == QMessageBox::Yes) { userMode->removeRow(currentrow); QString name = userMode->data(userMode->index(currentrow,0)).toString();//獲取姓名 QString del = "delete from user where name='" + name + "';"; query.exec(del); userMode->setQuery("select * from user"); ui->tableView->setModel(userMode); QMessageBox::information(this, "刪除成功", "所選信息刪除成功"); } } else QMessageBox::information(this, "提示", "請選擇你要刪除的信息行"); }
mainwidow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QMessageBox> #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug> #include <QSqlQueryModel> 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 ReshowTable(); void on_pushButton_connectDataBase_clicked(); void on_pushButton_add_clicked(); void on_pushButton_delete_clicked(); void on_pushButton_change_clicked(); void on_pushButton_query_clicked(); void on_tableView_pressed(const QModelIndex &index); private: Ui::MainWindow *ui; QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL"); QSqlQueryModel *userMode;//數據模型 }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); userMode = new QSqlQueryModel(ui->tableView);//綁定 } MainWindow::~MainWindow() { delete ui; } void MainWindow::ReshowTable() { userMode->setHeaderData(0,Qt::Horizontal,tr("姓名")); userMode->setHeaderData(1,Qt::Horizontal,tr("年齡")); userMode->setHeaderData(2,Qt::Horizontal,tr("性別")); userMode->setHeaderData(3,Qt::Horizontal,tr("手機號")); ui->tableView->setModel(userMode); } void MainWindow::on_pushButton_connectDataBase_clicked() { //連接數據庫 db.setHostName("127.0.0.1"); db.setPort(3306); db.setDatabaseName("test");//數據庫名稱 db.setUserName("root");//用戶名 db.setPassword("root");//密碼 qDebug()<<"available drivers:"; QStringList drivers = QSqlDatabase::drivers(); for(auto driver: drivers) qDebug() << driver; bool ok = db.open(); if(ok) { QMessageBox::information(this, "提示","數據庫連接成功"); qDebug()<<"數據庫連接成功"; } else { QMessageBox::information(this, "提示","數據庫連接失敗"); this->close(); qDebug()<<"數據庫連接失敗"; } //若數據庫中沒有表,則新建 QSqlQuery query; QString createTableUser="create table if not exists user(name varchar(8),age int(255), " "sex varchar(8), number int(255));"; query.exec(createTableUser); } //添加 void MainWindow::on_pushButton_add_clicked() { QString name = ui->lineEdit_name->text(); QString age = ui->lineEdit_age->text(); QString sex = ui->lineEdit_sex->text(); QString number = ui->lineEdit_number->text(); QSqlQuery query; QString str1 = "'" + name +"','" + age + "','" +sex+"','"+number+"'"; QString str = "insert into user(name, age, sex, number) values(" + str1 + ");"; query.exec(str); userMode->setQuery("select * from user;"); ReshowTable(); } //刪除 void MainWindow::on_pushButton_delete_clicked() { QSqlQuery query; int currentrow = ui->tableView->currentIndex().row(); userMode->removeRow(currentrow); if(currentrow != -1)//若選中一行 { int ok = QMessageBox::warning(this,tr("刪除當前行!"),tr("你確定刪除當前行嗎?"), QMessageBox::Yes,QMessageBox::No); //如確認刪除 if(ok == QMessageBox::Yes) { userMode->removeRow(currentrow); QString name = userMode->data(userMode->index(currentrow,0)).toString();//獲取姓名 QString del = "delete from user where name='" + name + "';"; query.exec(del); userMode->setQuery("select * from user"); ui->tableView->setModel(userMode); QMessageBox::information(this, "刪除成功", "所選信息刪除成功"); } } else QMessageBox::information(this, "提示", "請選擇你要刪除的信息行"); } //修改 void MainWindow::on_pushButton_change_clicked() { QSqlQuery query; QString str = "update user set name='" + ui->lineEdit_name->text() + "',age='" + ui->lineEdit_age->text() + "',sex='" + ui->lineEdit_sex->text() + "' where number='" + ui->lineEdit_number->text() + "';"; query.exec(str); QMessageBox::information(this, "修改成功", "表信息修改成功"); userMode->setQuery("select * from user;"); ReshowTable(); } //查詢 void MainWindow::on_pushButton_query_clicked() { userMode->setQuery("select * from user;"); ReshowTable(); } void MainWindow::on_tableView_pressed(const QModelIndex &index) { QString name = userMode->data(userMode->index(index.row(), 0)).toString(); QString age = userMode->data(userMode->index(index.row(), 1)).toString(); QString sex = userMode->data(userMode->index(index.row(), 2)).toString(); QString number = userMode->data(userMode->index(index.row(), 3)).toString(); ui->lineEdit_name->setText(name); ui->lineEdit_age->setText(age); ui->lineEdit_sex->setText(sex); ui->lineEdit_number->setText(number); }
感謝各位的閱讀,以上就是“Qt怎么連接數據庫并實現數據庫增刪改查”的內容了,經過本文的學習后,相信大家對Qt怎么連接數據庫并實現數據庫增刪改查這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。