QT5连接MySQL实现增删改查
音视频开发老舅 人气:0实现代码
#include #include #include #include #include #include #include #include using namespace std; #include #include #include const char *kDbType = "QMYSQL"; // 数据库类型 const QString& kHost = "127.0.0.1"; // 数据库的ipd地址 const int kPort = 3306; // 数据库端口号 const QString& kUsername = "root"; // 数据库账号 const QString& kPassword = "123456"; // 数据库密码 //const QString& dbname = "test"; // 数据库名 class DBSql{ private: QSqlDatabase db; // 数据库操作对象 QSqlQuery query; QSqlError error; // SQLError对象 bool flag; // 判断函数是否执行 bool DBInit(){ db = QSqlDatabase::addDatabase(kDbType); db.setHostName(kHost); db.setPort(kPort); db.setUserName(kUsername); db.setPassword(kPassword); QString sqlname = "数据库连接"; 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: // 构造函数,初始化数据库连接 DBSql(){ this->DBInit(); } // 设置数据库名 void setDatabase(const QString& dbname){ db.setDatabaseName(dbname); db.open(); } // 查询表字段数 int sqlRecord(const QString& sql){ QSqlQuery query(sql); QSqlRecord record = query.record(); int const n = record.count(); qDebug()<<"查询的字段总数:"<< n; return n; } // 查询结果总数 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<
运行结果
测试数据
/* 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;
加载全部内容