240717班级,工业化控制系统,煤矿相关行业,昆仑系统
wxx
2024-10-31 0a648f0c590356672421eb09fb2e15fc0e91f5ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#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;
 
class MysqlConn
{
private:
    // ¾ø¶ÔʱÖÓ
    steady_clock::time_point    m_alivetime;
    // Á¬½Ó
    MYSQL* m_conn;
    // ²éѯµÄ½á¹û¼¯
    MYSQL_RES* m_result;
    // µ¥¼Ç¼½á¹û¼¯
    MYSQL_ROW                    m_mysqlRow;
 
    // ½á¹û¼¯ÊÍ·Å
    void freeRes();
    // µ¼³öijһÕűíÖеÄÊý¾Ý
    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 query(const string sql);
    // ±éÀú²éѯ½á¹û¼¯
    bool getRes();
    // »ñÈ¡½á¹û¼¯ÖеÄ×Ö¶ÎÖµ
    string getValue(const int fieldIndex) const;
    // Çл»Êý¾Ý¿â
    bool selectDB(const string dbName) const;
    // ½¨¿â
    //bool createDB(const string dbName) const;
    // ±¸·Ýij¸ö¿â
    void backupCurrentDB(const string path);
    // ÊÂÎñ²Ù×÷
    bool transaction() const;
    // Ìá½»ÊÂÎñ
    bool commit() const;
    // ÊÂÎñ»Ø¹ö
    bool rollback() const;
    // Ë¢ÐÂÆðʼµÄ¿ÕÏÐʱ¼äµã
    void refreashAliveTime();
    // ¼ÆËã´æ»î×Üʱ³¤
    ll getAliveTime();
};