From cec6937344e179a6db50c6386b24afcff165561f Mon Sep 17 00:00:00 2001 From: zyf <2786722087@qq.com> Date: 星期三, 02 四月 2025 09:21:04 +0800 Subject: [PATCH] zyf_0402_log --- Server/张怡帆/code/ConnectMysql.h | 61 ++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 0 deletions(-) diff --git "a/Server/\345\274\240\346\200\241\345\270\206/code/ConnectMysql.h" "b/Server/\345\274\240\346\200\241\345\270\206/code/ConnectMysql.h" new file mode 100644 index 0000000..21d5784 --- /dev/null +++ "b/Server/\345\274\240\346\200\241\345\270\206/code/ConnectMysql.h" @@ -0,0 +1,61 @@ +#include <iostream> +//#include <mysql.h> +#include <winsock2.h> +#include "mysql/include/mysql.h" + +#include <memory> +#include <string> +#include <chrono> +#include <fstream> +#include <vector> + +using namespace std; + +#pragma comment(lib, "libmysql.lib") +#pragma comment(lib, "ws2_32.lib") + +typedef long long ll; +/* +mysql_init() 初始化MYSQL变量 +mysql_real_connect //连接到数据库 +mysql_real_query //数据库查询 +mysql_query(MYSQL*mysql,const char*query) +mysql_store_result/mysql_use_result //返回的获取查询结果数据 +mysql_fetch_row//读取结果集数据 +mysql_free_result//释放结果集,以防内存泄漏 +mysql_close//关闭数据库连接 + +*/ +class ConnectMysql +{ +private: + std::chrono::steady_clock::time_point m_alivetime;// 记录连接的存活起始时间点 + MYSQL* m_conn; + MYSQL_RES* m_result;// 保存查询结果 + MYSQL_ROW m_mysqlRow;// 保存查询结果的某一行 + + void freeRes();// 释放结果集 + +public: + ConnectMysql(); + ~ConnectMysql(); + bool connect(const string user = "root", const string passwd = "1", + const string dbName = "AI_datas", string ip = "192.168.133.129", const unsigned short port = 3306);// 连接数据库 + bool update(const string sql) const;// 更新数据库 + bool query(const string sql);// 查询数据库 + bool getRes();// 遍历查询结果集,获取下一行数据 + string getValue(const int fieldIndex) const;// 获取查询结果集某一行的某一列 + vector<vector<string>> getQueryResults(); // 查询结果 + //bool selectDB(const string dbName) const;// 选择数据库 + //bool createDB(const string dbName) const;// 创建数据库 + + void backupCurrentDBUsingShell(const string path); // 使用 Shell 脚本备份数据库 + void backupCurrentDB(const string path); //备份数据库 + void backupCurrentTable(const string path, const string tableName); + bool transaction() const;// 开启事务 + bool commit() const;// 提交事务 + bool rollback() const;// 回滚事务 + void refreashAliveTime();// 刷新存活时间 + ll getAliveTime();// 获取存活时间,单位:毫秒 +}; + -- Gitblit v1.8.0