-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCourseDao.cpp
188 lines (126 loc) · 4.25 KB
/
CourseDao.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
#include "CourseDao.h"
int CourseDao::AddCourse(Course c)
{
//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 course (name,score,value) values ('%s',%d,%d)",
c.getName().c_str(), c.getScore(),c.getValue());
//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 CourseDao::DelCourse(int cid)
{
//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 course where id = %d", cid);
//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 CourseDao::UpdateCourse(Course course)
{
//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 course set name='%s',score=%d,value=%d where id = %d",
course.getName().c_str(),course.getScore(),course.getValue(),course.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;
}
vector<Course> CourseDao::GetAllCourse()
{
vector<Course>cList;
//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 course");
//execute query
if (mysql_query(con, sql)) {
fprintf(stderr, "Failed to query data :Error %s", mysql_error(con));
printf("≤È—Ø ß∞‹£°\n");
return vector<Course>();
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
Course c;
c.setId(atoi(row[0]));
c.setName(row[1]);
c.setScore(atoi(row[2]));
c.setValue(atoi(row[3]));
cList.push_back(c);
}
return cList;
}
vector<Course> CourseDao::GetAllCourseByName(char* cname)
{
vector<Course>cList;
//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 course where name like '%%%s%%'", cname);
//execute query
if (mysql_query(con, sql)) {
fprintf(stderr, "Failed to query data :Error %s", mysql_error(con));
printf("≤È—Ø ß∞‹£°\n");
return vector<Course>();
}
MYSQL_RES* res = mysql_store_result(con);
MYSQL_ROW row;
while ((row = mysql_fetch_row(res))) {
Course c;
c.setId(atoi(row[0]));
c.setName(row[1]);
c.setScore(atoi(row[2]));
c.setValue(atoi(row[3]));
cList.push_back(c);
}
return cList;
}