-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
56 lines (51 loc) · 1.2 KB
/
Copy pathConnection.cpp
File metadata and controls
56 lines (51 loc) · 1.2 KB
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
#include "Connection.h"
#include "public.h"
#include <iostream>
Connection::Connection()
{
_conn = mysql_init(nullptr);
if (_conn == nullptr)
{
std::cerr << "mysql_init() failed\n";
exit(EXIT_FAILURE);
}
}
Connection::~Connection()
{
if (_conn != nullptr)
mysql_close(_conn);
}
bool Connection::connect(std::string ip, unsigned short port, std::string user, std::string password,
std::string dbname)
{
MYSQL *p = mysql_real_connect(_conn, ip.c_str(), user.c_str(),
password.c_str(), dbname.c_str(), port, nullptr, 0);
return p != nullptr;
}
// 更新操作 insert、delete、update
bool Connection::update(std::string sql)
{
if (mysql_query(_conn, sql.c_str()))
{
LOG("更新失败:" + sql);
return false;
}
return true;
}
// 查询操作 select
MYSQL_RES *Connection::query(std::string sql)
{
if (mysql_query(_conn, sql.c_str()))
{
LOG("查询失败:" + sql);
return nullptr;
}
return mysql_use_result(_conn);
}
void Connection::refreshAliveTime(){
_alivetime=clock();
}
clock_t Connection::getAliveTime() const
{
return clock()-_alivetime;
}