forked from BRILLANT-1/ConnectionPool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
42 lines (42 loc) · 934 Bytes
/
Connection.cpp
File metadata and controls
42 lines (42 loc) · 934 Bytes
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
#include"Public.h"
#include"Connection.h"
#include<iostream>
#include<string>
#include<cstdio>
#include<mysql.h>
using namespace std;
Connection::Connection()
{
_conn = mysql_init(nullptr);
}
Connection::~Connection()
{
if (_conn != nullptr)
mysql_close(_conn);
}
bool Connection::connect(string ip, unsigned short port, string username, string password, string dbname)
{
MYSQL * P = mysql_real_connect(_conn, ip.c_str(), username.c_str(), password.c_str(), dbname.c_str(),
port,nullptr,0);
return P != nullptr;
}
bool Connection::update(string sql)
{
if (mysql_query(_conn, sql.c_str()))
{
cout<<"error"<< mysql_error(_conn)<<"\n";
LOG("¸üÐÂʧ°Ü£º"+ sql);
mysql_error(_conn);
return false;
}
return true;
}
MYSQL_RES* Connection::query(string sql)
{
if (mysql_query(_conn, sql.c_str()))
{
LOG("¸üÐÂʧ°Ü£º" + sql);
return nullptr;
}
return mysql_use_result(_conn);
}