| | |
| | | #pragma once |
| | | #include<iostream> |
| | | #include <memory> |
| | | #include <string> |
| | | #include <mysql.h> |
| | | #include <chrono> // æ¶é |
| | | #include <fstream> |
| | | |
| | | #pragma comment(lib, "./lib/libmysql.lib") // å è½½æ°æ®åºåºæä»¶ |
| | | using namespace std; |
| | | using namespace chrono; |
| | | typedef long long ll; |
| | | #include <string> |
| | | #include <WinSock2.h> |
| | | //#include <mysql.h> |
| | | #include "MySQL/include/mysql.h" |
| | | #include <string.h> |
| | | #include <chrono> |
| | | |
| | | #pragma comment(lib,"./MySQL/lib/libmysql.lib") |
| | | |
| | | class MysqlConn |
| | | { |
| | | private: |
| | | // ç»å¯¹æ¶é |
| | | steady_clock::time_point m_alivetime; |
| | | // è¿æ¥ |
| | | MYSQL* m_conn; |
| | | // æ¥è¯¢çç»æé |
| | | MYSQL_RES* m_result; |
| | | // åè®°å½ç»æé |
| | | MYSQL_ROW m_mysqlRow; |
| | | |
| | | // ç»æééæ¾ |
| | | void freeRes(); |
| | | // å¯¼åºæä¸å¼ 表ä¸çæ°æ® |
| | | void backupCurrentTable(const string path, const string tableName); |
| | | public: |
| | | // åå§åæ°æ®åº |
| | | // åå§åè¿æ¥ |
| | | MysqlConn(); |
| | | // æ°æ®åºè¿æ¥éæ¾ |
| | | ~MysqlConn(); |
| | | // è¿æ¥æ°æ®åº, éæä¾ç¨æ· å¯ç æ°æ®åºåç§° ip ç«¯å£ |
| | | bool connect(const string user, const string passwd, \ |
| | | const string dbName, string ip, \ |
| | | const unsigned short port = 3306U); |
| | | // æ´æ°æ°æ®åº:å¢å æ¹æä½ |
| | | bool update(const string sql) const; |
| | | // è¿æ¥æ°æ®åº |
| | | bool connect(std::string ip, std::string userName, std::string passwd, std::string db , int port = 3306); |
| | | // éæ¾èµæº |
| | | ~MysqlConn( ); |
| | | // æ´æ°æ°æ®åº ( insert update delete ) |
| | | bool update(std::string sql); |
| | | // æ¥è¯¢æ°æ®åº |
| | | bool query(const string sql); |
| | | // éåæ¥è¯¢ç»æé |
| | | bool getRes(); |
| | | // è·åç»æéä¸çåæ®µå¼ |
| | | string getValue(const int fieldIndex) const; |
| | | // åæ¢æ°æ®åº |
| | | bool selectDB(const string dbName) const; |
| | | // å»ºåº |
| | | //bool createDB(const string dbName) const; |
| | | // å¤ä»½æä¸ªåº |
| | | void backupCurrentDB(const string path); |
| | | bool query(std::string sql); |
| | | // å¾å°ç»æé |
| | | bool getResult(); |
| | | // è·åç»æéçåæ®µ |
| | | std::string getField(int index); |
| | | // äºå¡æä½ |
| | | bool transaction() const; |
| | | bool transaction(); |
| | | // æäº¤äºå¡ |
| | | bool commit() const; |
| | | bool commit( ); |
| | | // äºå¡åæ» |
| | | bool rollback() const; |
| | | // å·æ°èµ·å§çç©ºé²æ¶é´ç¹ |
| | | void refreashAliveTime(); |
| | | // 计ç®åæ´»æ»æ¶é¿ |
| | | ll getAliveTime(); |
| | | bool rollback(); |
| | | |
| | | void refreshActiveTime( ); // å·æ°æ´»è·æ¶é´ |
| | | long long getActiveTime(); // è·åå½åæ´»è·çæ¶é´é´é |
| | | // å®å
¨æ ¡éªæ¥å£ |
| | | bool isSqlSafe(const std::string& sql); |
| | | |
| | | private: |
| | | |
| | | void freeRes(); |
| | | MYSQL* mysql_ = NULL ; |
| | | MYSQL_RES* res_ = NULL; |
| | | MYSQL_ROW row_ = nullptr; |
| | | std::chrono::steady_clock::time_point activeTime_; |
| | | }; |
| | | |