-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStudentDao.cpp
238 lines (169 loc) · 5.85 KB
/
StudentDao.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "StudentDao.h"
vector<Student> StudentDao::GetAllStudent() {
//declare a sql sentence
char sql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(sql, "select * from student");
//execute query
if (mysql_query(con, sql)) {
fprintf(stderr, "Failed to query data :Error %s", mysql_error(con));
printf("查询失败! \n");
return vector<Student>();
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
vector<Student>stuList;
while ((row = mysql_fetch_row(res))) {
Student stu;
stu.setId(atoi(row[0]));
stu.setSname(row[1]);
stu.setSex(row[2]);
stu.setAge(atoi(row[3]));
stu.setNative(row[4]);
stu.setDept(row[5]);
stuList.push_back(stu);
// cout<<"here!"<<endl;
}
return stuList;
}
int StudentDao::AddStudent(Student student) {
//declare a sql sentence
char insertSql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(insertSql, "insert into student (sname,sex,age,native,dept) values ('%s','%s',%d,'%s','%s')",
student.getSname().c_str(), student.getSex().c_str(), student.getAge(), student.getNative().c_str(), student.getDept().c_str());
//execute query
if (mysql_query(con, insertSql)) {
fprintf(stderr, "Failed to insert data :Error %s", mysql_error(con));
printf("插入失败!\n");
return 0;
}
return 1;
}
int StudentDao::DelStudent(int id) {
//declare a sql sentence
char delSql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(delSql, "delete from student where id = %d", id);
printf("\ndelSql\n: %s", delSql);
//execute query
if (mysql_query(con, delSql)) {
fprintf(stderr, "Failed to delete data :Error %s", mysql_error(con));
printf("删除失败!\n");
return 0;
}
return 1;
}
int StudentDao::UpdateStudent(Student student) {
//declare a sql sentence
char updateSql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(updateSql, "update student set sname='%s',sex='%s',age=%d,native='%s',dept='%s' where id = %d",
student.getSname().c_str(), student.getSex().c_str(), student.getAge(), student.getNative().c_str(), student.getDept().c_str(), student.getId());
//execute query
if (mysql_query(con, updateSql)) {
fprintf(stderr, "Failed to insert data :Error %s", mysql_error(con));
printf("插入失败!\n");
return 0;
}
return 1;
}
Student StudentDao::CheckById(int id)
{
//declare a sql sentence
char sql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(sql, "select * from student where id = %d",id);
//execute query
if (mysql_query(con, sql)) {
fprintf(stderr, "Failed to query data :Error %s", mysql_error(con));
printf("查询失败!\n");
Student s;
//a special mark
s.setId(-1);
return s;
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
vector<Student>stuList;
while ((row = mysql_fetch_row(res))) {
Student stu;
stu.setId(atoi(row[0]));
stu.setSname(row[1]);
stu.setSex(row[2]);
stu.setAge(atoi(row[3]));
stu.setNative(row[4]);
stu.setDept(row[5]);
stuList.push_back(stu);
// cout<<"here!"<<endl;
}
if(stuList.size()>0)
return stuList[0];
Student s;
s.setId(-1);
return s;
}
vector<Student> StudentDao::CheckByName(char* name) {
//declare a sql sentence
char sql[256];
ReadProperties::InitProperties(host, user, pw, dbName, port);
// cout<<"come to here!"<<endl;
ConnectDB connectDb(host, user, pw, dbName, port);
connectDb.connect();
MYSQL* con = connectDb.getCon();
// cout<<"come to here!"<<endl;
//using sprintf to init sql
sprintf(sql, "select * from student where sname like '%%%s%%'", name);
printf("\n模糊查询的sql:%s\n", sql);
//execute query
if (mysql_query(con, sql)) {
fprintf(stderr, "Failed to query data :Error %s", mysql_error(con));
printf("查询失败!\n");
return vector<Student>();
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
vector<Student>stuList;
while ((row = mysql_fetch_row(res))) {
Student stu;
stu.setId(atoi(row[0]));
stu.setSname(row[1]);
stu.setSex(row[2]);
stu.setAge(atoi(row[3]));
stu.setNative(row[4]);
stu.setDept(row[5]);
stuList.push_back(stu);
// cout<<"here!"<<endl;
}
return stuList;
}