zyf
2025-04-02 72ca2f2f85c80572a6d689d785567fb03b388c67
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Database_Package.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
 
#include <iostream>
//#include <mysql.h>
#include <winsock2.h>
#include "mysql/include/mysql.h"
 
#include <memory>
#include <string>
#include <chrono> 
#include <fstream>
#include "ConnectionPool.h"
 
using namespace std;
 
/*
#define M_MYSQL_DNAME AI_datas
#define M_MYSQL_IP 192.168.133.129
#define M_MYSQL_PORT 3306
#define M_MYSQL_USER root
#define M_MYSQL_PASSWD 1
#define M_MYSQL_PATH D:\\PROGECT_0214
*/
 
void testDatabaseConnection()
{
    // 创建连接池对象
    ConnectionPool* pool = ConnectionPool::getInstance();
 
    // 从连接池中获取连接
    shared_ptr<ConnectMysql> conn = pool->getConnection();
    if (conn == nullptr)
    {
        cerr << "未取得数据库连接!" << endl;
        return;
    }
    cout << "连接成功" << endl;
 
    // 测试更新功能
    string sql = "INSERT INTO employee_info (employee_name, employee_age, employee_sex, employee_bir, employee_password, employee_email, employee_phone, employee_department)VALUES('朱琛', 20, '男', '1999-09-09', '123456', '123456@123.com', '12345678901', 'asdf')";
    if (conn->update(sql))
    {
        cout << "更新成功!" << endl;
    }
    else
    {
        cerr << "更新失败!" << endl;
    }
 
    // 测试查询功能
    sql = "SELECT * FROM employee_info";
    if (conn->query(sql))
    {
        cout << "正在查询..." << endl;
        // 获取查询结果
        vector<vector<string>> results = conn->getQueryResults();
        // 处理查询结果
        for (const auto& row : results)
        {
            for (const auto& field : row)
            {
                cout << field << "\t";
            }
            cout << endl;
        }
    }
    else
    {
        cerr << "查询失败!" << endl;
    }
 
    // 实现备份功能
    string backupPath = "D:\\PROGECT_0214\\";  // 备份路径,你可以根据需要修改
    cout << "开始备份数据库..." << endl;
    conn->backupCurrentDB(backupPath);
    cout << "数据库备份完成!" << endl;
 
    cout << "开始备份表 employee_info..." << endl;
    conn->backupCurrentTable(backupPath, "employee_info");
    cout << "表 employee_info 备份完成!" << endl;
    // 使用shell脚本备份数据库
//string backupPath = "D:\\PROGECT_0214\\";  // 备份路径,你可以根据需要修改
//cout << "开始使用shell脚本备份数据库..." << endl;
//conn->backupCurrentDBUsingShell(backupPath);
//cout << "数据库备份完成!" << endl;
}
 
int main()
{
    testDatabaseConnection();
    return 0;
}