欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT5連接MySQL實現(xiàn)增刪改查

 更新時間:2022年12月28日 09:50:46   作者:音視頻開發(fā)老舅  
這篇文章主要為大家詳細介紹了QT5如何連接MySQL實現(xiàn)增刪改查功能,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的可以了解一下

實現(xiàn)代碼

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include 
#include 
#include 
 
const char *kDbType = "QMYSQL";      // 數(shù)據庫類型
const QString& kHost = "127.0.0.1";  // 數(shù)據庫的ipd地址
const int kPort = 3306;              // 數(shù)據庫端口號
const QString& kUsername = "root";   // 數(shù)據庫賬號
const QString& kPassword = "123456"; // 數(shù)據庫密碼
//const QString& dbname = "test";    // 數(shù)據庫名
 
class DBSql{
 
private:
    QSqlDatabase db;    // 數(shù)據庫操作對象
    QSqlQuery query;
    QSqlError error;    // SQLError對象
    bool flag;          // 判斷函數(shù)是否執(zhí)行
    bool DBInit(){
        db = QSqlDatabase::addDatabase(kDbType);
        db.setHostName(kHost);
        db.setPort(kPort);
        db.setUserName(kUsername);
        db.setPassword(kPassword);
        QString sqlname = "數(shù)據庫連接";
        flag = db.open();
        sqlerror(flag,query,NULL,sqlname);
        return flag;
    }
    void sqlerror(bool flag,QSqlQuery query,QString sql,QString sqlname){
        if(flag){
            qDebug() << sqlname+"成功!" << sql;
        }else{
            error = query.lastError();
            qDebug() << sqlname+"失??!" << sql << error.driverText() << error.databaseText();
        }
    }
public:
 
    // 構造函數(shù),初始化數(shù)據庫連接
    DBSql(){
        this->DBInit();
    }
    // 設置數(shù)據庫名
    void setDatabase(const QString& dbname){
        db.setDatabaseName(dbname);
        db.open();
    }
    // 查詢表字段數(shù)
    int sqlRecord(const QString& sql){
        QSqlQuery query(sql);
        QSqlRecord record = query.record();
        int const n = record.count();
        qDebug()<<"查詢的字段總數(shù):"<< n;
        return n;
    }
    // 查詢結果總數(shù)
    int sqlTotal(const QString& sql){
        QSqlQuery query(sql);
        return query.size();
    }
    // 根據sql返回查詢結果
    QString sqlQueryValue(const QString& sql){
        QSqlQuery query(db);
        flag = query.exec(sql);
        if(flag && query.next()){
            return query.value(0).toString();
        }else{
            sqlerror(false,query,sql,"返回查詢結果失敗");
            return NULL;
        }
    }
    // 增加記錄
    bool sqlInsert(const QString& sql){
        QSqlQuery query(db);
        flag = query.exec(sql);
        sqlerror(flag,query,sql,"增加記錄");
        return flag;
    }
    // 刪除記錄
    bool sqlDelete(const QString& sql){
        QSqlQuery query(db);
        flag = query.exec(sql);
        sqlerror(flag,query,sql,"刪除記錄");
        return flag;
    }
    // 查詢記錄
    list* sqlQuery1(const QString& sql){
        db.open();
        QSqlQuery query(db);
        query.exec(sql);
        const int count = query.record().count();
        const int total = query.size();
        cout<<"The number of fileds to query: "<* dot;
        int *in;
        dot = new list;
        int i = 0;
        while(query.next()){
            int j = 0;
            in = new int[4];
            while(jpush_back(in);
        sqlerror(flag,query,sql,"查詢記錄");
        return dot;
        }
    }
 
    int(*f(const QString& sql,const int total))[4]{
        db.open();
        QSqlQuery query(db);
        query.exec(sql);
        const int count = query.record().count();
        cout<<"The number of fileds to query: "<* li;
//    li = dbsql.sqlQuery1(sqlquery);
//    qDebug()<size();
//    list::iterator iterator;
//    for(iterator=li->begin();iterator!=li->end();++iterator){
//        cout<<*iterator<

運行結果

測試數(shù)據

/*
 Navicat Premium Data Transfer
 Source Server         : localhost_3306
 Source Server Type    : MySQL
 Source Server Version : 50556
 Source Host           : localhost:3306
 Source Schema         : test
 Target Server Type    : MySQL
 Target Server Version : 50556
 File Encoding         : 65001
 Date: 23/03/2020 12:09:27
*/
 
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
 
-- ----------------------------
-- Table structure for xy
-- ----------------------------
DROP TABLE IF EXISTS `xy`;
CREATE TABLE `xy`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `x1` int(255) NULL DEFAULT NULL,
  `y1` int(255) NULL DEFAULT NULL,
  `x2` int(255) NULL DEFAULT NULL,
  `y2` int(255) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
 
-- ----------------------------
-- Records of xy
-- ----------------------------
INSERT INTO `xy` VALUES (1, 1, 12, 3, 4);
INSERT INTO `xy` VALUES (2, 5, 6, 7, 8);
INSERT INTO `xy` VALUES (3, 9, 10, 11, 12);
INSERT INTO `xy` VALUES (7, 13, 14, 15, 16);
INSERT INTO `xy` VALUES (8, 17, 18, 19, 20);
INSERT INTO `xy` VALUES (9, 21, 22, 23, 24);
 
SET FOREIGN_KEY_CHECKS = 1;

到此這篇關于QT5連接MySQL實現(xiàn)增刪改查的文章就介紹到這了,更多相關QT5 MySQL增刪改查內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 微軟Detours Hook庫編譯與使用教程

    微軟Detours Hook庫編譯與使用教程

    Detours 是一個兼容多個Windows系列操作系統(tǒng)版本(包括 Windows XP 到 Windows 11)的工具庫,Detours 是微軟開發(fā)的一個強大的Windows API鉤子庫,用于監(jiān)視和攔截函數(shù)調用,這篇文章給大家介紹微軟Detours Hook庫編譯與使用,感興趣的朋友一起看看吧
    2024-08-08
  • 全局變量與局部變量在內存中的區(qū)別詳細解析

    全局變量與局部變量在內存中的區(qū)別詳細解析

    以下是對全局變量與局部變量在內存中的區(qū)別進行了詳細的總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-10-10
  • C++中的函數(shù)知識點大全

    C++中的函數(shù)知識點大全

    這篇文章介紹了C++中的函數(shù)知識點,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • C語言實現(xiàn)三子棋源代碼

    C語言實現(xiàn)三子棋源代碼

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)三子棋源代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C++虛函數(shù)注意事項

    C++虛函數(shù)注意事項

    這篇文章主要給大家分享了EC++虛函數(shù)注意事項,
    2022-01-01
  • C++中智能指針如何設計和使用

    C++中智能指針如何設計和使用

    智能指針(smart pointer)是存儲指向動態(tài)分配(堆)對象指針的類,用于生存期控制,能夠確保自動正確的銷毀動態(tài)分配的對象,防止內存泄露,需要的朋友可以參考下
    2012-11-11
  • C語言實現(xiàn)三子棋小游戲的示例代碼

    C語言實現(xiàn)三子棋小游戲的示例代碼

    這篇文章主要介紹了如何通過C語言實現(xiàn)三子棋小游戲,三子棋小游戲的實現(xiàn)主要依賴于循環(huán)語句、函數(shù)和數(shù)組,感興趣的小伙伴可以嘗試一下
    2022-10-10
  • C++之openFrameworks框架介紹

    C++之openFrameworks框架介紹

    本章我們將介紹一個非常好用的跨平臺的 C++開源框架 openFrameworks。它是一個開源的跨平臺的C++工具包,方便開發(fā)者創(chuàng)建出一個更簡單和直觀的框架,擅長開發(fā)圖像和動畫,感興趣的同學可以參考一下
    2023-05-05
  • c實現(xiàn)linux下的數(shù)據庫備份

    c實現(xiàn)linux下的數(shù)據庫備份

    本文給大家簡單介紹下c實現(xiàn)linux下的數(shù)據庫備份的方法和具體的源碼,十分的實用,有需要的小伙伴可以參考下。
    2015-07-07
  • C++ Coroutine簡單學習教程

    C++ Coroutine簡單學習教程

    這篇文章主要為大家詳細介紹了C++ Coroutine的簡單學習教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論