-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnectDB.cpp
68 lines (53 loc) · 1.33 KB
/
ConnectDB.cpp
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
#include "ConnectDB.h"
const char* ConnectDB::getHost() const {
return host;
}
void ConnectDB::setHost(const char* host) {
ConnectDB::host = host;
}
const char* ConnectDB::getUser() const {
return user;
}
void ConnectDB::setUser(const char* user) {
ConnectDB::user = user;
}
const char* ConnectDB::getPw() const {
return pw;
}
void ConnectDB::setPw(const char* pw) {
ConnectDB::pw = pw;
}
const char* ConnectDB::getDbName() const {
return dbName;
}
void ConnectDB::setDbName(const char* dbName) {
ConnectDB::dbName = dbName;
}
const int ConnectDB::getPort() const {
return port;
}
MYSQL* ConnectDB::getCon() const {
return con;
}
void ConnectDB::setCon(MYSQL* con) {
ConnectDB::con = con;
}
ConnectDB::ConnectDB(const char* host, const char* user, const char* pw, const char* dbName, const int port) : host(host),
user(user),
pw(pw),
dbName(dbName),
port(port) {}
void ConnectDB::connect() {
//ÉùÃ÷Êý¾Ý¿âÁ¬½ÓÖ¸Õë
con = mysql_init(NULL);
//ÉèÖñàÂë
mysql_options(con, MYSQL_SET_CHARSET_NAME, "GBK");
if (!mysql_real_connect(con, host, user, pw, dbName, port, NULL, 0)) {
printf("Failed to connect!%s\n", mysql_errno(con));
return;
}
}
ConnectDB::~ConnectDB() {
mysql_close(con);
cout << "Êý¾Ý¿âÁ¬½ÓÒѾ¹Ø±Õ£¡\n";
}