-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_operate.cpp
130 lines (112 loc) · 3.62 KB
/
db_operate.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<string>
#include<cstdlib>
#include<stdio.h>
#include<mysql++/mysql++.h>
#include<vector>
using namespace std;
#define MYSQL_USER "root"
#define MYSQL_PASSWD "123456"
#define MYSQL_PORT 3306
#define CMD_CREATE_USER_TABLE "CREATE TABLE user ("\
"user_id INT NOT NULL AUTO_INCREMENT,"\
"uname VARCHAR(100) NOT NULL,"\
"pwd VARCHAR(100) NOT NULL,"\
"PRIMARY KEY(user_id));"
bool judgeConnectStatus(mysqlpp::Connection& con, char* db_name, char* db_host, char* username, char* password, unsigned int port);
void createTable(string cmd, char* username, char* password, unsigned int port);
bool queryData(char* username, char* password, unsigned int port, char* db_name, char* db_host, string table_name, vector<string> data);
bool insertData(char* username, char* password, unsigned int port, char* db_name, char* db_host, string table_name, vector<string> data);
bool judgeConnectStatus(mysqlpp::Connection& con, char* db_name, char* db_host, char* username, char* password, unsigned int port)
{
if(!con.connect(db_name, db_host, username, password, port))
{
char statusMessageFail[100];
sprintf(statusMessageFail, "connect database:%s with user %s fail! Please check parameters.", db_name, username);
cout << statusMessageFail << endl;
return false;
}
else
{
char statusMessageSuccess[100];
sprintf(statusMessageSuccess, "connect database:%d with user %d success!", MYSQL_PORT, MYSQL_PORT);
cout << statusMessageSuccess<< endl;
cout << CMD_CREATE_USER_TABLE <<endl;
return true;
}
}
void createTable(string cmd, char* username, char* password, unsigned int port)
{
mysqlpp::Connection con(false);
con.set_option(new mysqlpp::SetCharsetNameOption("utf8"));
char* db_name = (char*)"iwen";
char* db_host = (char*)"localhost";
if(!judgeConnectStatus(con, db_name, db_host, username, password, port))
{
cout << "create table failed!"<< endl;
}
else
{
mysqlpp::Query query=con.query(cmd);
bool result=query.exec();
if(result)
{
cout << "create table success!" << endl;
}
else
{
cout << "create table failed!!!"<< endl;
}
}
}
bool queryData(char* username, char* password, unsigned int port, char* db_name, char* db_host, string table_name, string cmd)
{
mysqlpp::Connection con(false);
con.set_option(new mysqlpp::SetCharsetNameOption("utf8"));
if(!judgeConnectStatus(con, db_name, db_host, username, password, port))
{
return false;
}
else
{
mysqlpp::Query query=con.query(cmd);
mysqlpp::StoreQueryResult result=query.store();
if(nullptr==result){
cout<<"query failed!"<<endl;
return false;
}
else
{
cout << "query result is:" << endl;
for(auto iter=result.begin();iter!=result.end();++iter){
cout << "\t" << (*iter)[0] << endl;
}
return true;
}
}
}
bool insertData(char* username, char* password, unsigned int port, char* db_name, char* db_host, string table_name, vector<string> data)
{
if(table_name == "user")
{
mysqlpp::Connection con(false);
con.set_option(new mysqlpp::SetCharsetNameOption("utf8"));
if(!judgeConnectStatus(con, db_name, db_host, username, password, port))
{
return false;
}
else
{
// query.h define some function about insert data;
}
}
else
{
cout << "For now, only support table user!" << endl;
}
return true;
}
int main()
{
createTable(CMD_CREATE_USER_TABLE, (char*)MYSQL_USER, (char*)MYSQL_PASSWD, MYSQL_PORT);
}