-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataAccess.py
More file actions
270 lines (243 loc) · 9.36 KB
/
dataAccess.py
File metadata and controls
270 lines (243 loc) · 9.36 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import pypyodbc
import os
path = os.getcwd()
mdb = 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+ path +'\\WCA_A5.accdb'
conn = pypyodbc.win_connect_mdb(mdb)
cur = conn.cursor()
def login_check(identity, id, password):
print(identity, id, password)
if id == "" or password == "":
return False
if identity == "Competitor":
try:
query = "select password from Competitor where WCAid = %d " % (int(id))
except:
return False
elif identity == "Organizer":
try:
query = "select password from Organizer where oid = %d " % (int(id))
except:
return False
else:
return False
cur.execute(query)
if password == cur.fetchone()[0]:
return True
else:
return False
def getCompetition():
query = "select * from Competition"
result = []
for row in cur.execute(query):
result.append(row)
return result
def changeCompetition(cid, info, cname, from_time, to_time):
update = "update Competition set info='%s', cname='%s', from_time='%s',to_time='%s' where cid=%d" % (info, cname, from_time, to_time, cid)
cur.execute(update)
cur.commit()
def addCompetition(info, cname, from_time, to_time):
query = "select max(cid) from Competition"
cur.execute(query)
cid = cur.fetchone()[0] + 1
insert = "insert into Competition(cid,info,cname,from_time,to_time) values(%d,'%s','%s','%s','%s')" % (cid, info, cname, from_time, to_time)
cur.execute(insert)
cur.commit()
def getEvents():
query = "select * from Events"
results = []
for row in cur.execute(query):
results.append(row)
return results
def getCompetitionEvents(competition):
query = "select E.ename " \
"from Events E, Competition C, Competition_events CE " \
"where C.cname='%s' and E.eid=CE.eid and C.cid=CE.cid" % (competition)
results = []
for row in cur.execute(query):
results.append(row[0])
return results
def addEvent(event, competition):
query = "select eid from Events where ename='%s'" % (event)
cur.execute(query)
eid = cur.fetchone()[0]
query = "select cid from Competition where cname='%s'" % (competition)
cur.execute(query)
cid = cur.fetchone()[0]
insert = "insert into Competition_events values(%d,%d)" % (cid, eid)
cur.execute(insert)
cur.commit()
def deleteEvent(event, competition):
query = "select eid from Events where ename='%s'" % (event)
cur.execute(query)
eid = cur.fetchone()[0]
query = "select cid from Competition where cname='%s'" % (competition)
cur.execute(query)
cid = cur.fetchone()[0]
delete = "delete from Competition_events where cid=%d and eid=%d" % (cid, eid)
cur.execute(delete)
cur.commit()
def getCompetitionRecord(event, competition):
query = "select C.WCAid, C.name, CR.avg, CR.best " \
"from Competitor C, Competition_record CR, Events E, Competition Com " \
"where E.ename='%s' and Com.cname='%s' and E.eid=CR.eid and Com.cid=CR.cid " \
"and C.WCAid=CR.WCAid " \
"order by CR.avg" % (event, competition)
results = []
for row in cur.execute(query):
results.append(row)
return results
def changeCompetitionRecord(WCAid, competition, event, avg, best):
query = "select eid from Events where ename='%s'" % (event)
cur.execute(query)
eid = cur.fetchone()[0]
query = "select cid from Competition where cname='%s'" % (competition)
cur.execute(query)
cid = cur.fetchone()[0]
update = "update Competition_record CR set CR.avg=%.2f, CR.best=%.2f " \
"where CR.WCAid=%d and CR.eid=%d and CR.cid=%d" % (float(avg), float(best), WCAid, eid, cid)
cur.execute(update)
cur.commit()
def saveCompetitionRecord(WCAid, competition, event, avg, best):
query = "select eid from Events where ename='%s'" % (event)
cur.execute(query)
eid = cur.fetchone()[0]
query = "select cid from Competition where cname='%s'" % (competition)
cur.execute(query)
cid = cur.fetchone()[0]
insert = "insert into Competition_record values(%d,%d,%d,%s,%s)" % (eid, cid, WCAid, avg, best)
cur.execute(insert)
cur.commit()
def deleteCompetitionRecord(WCAid, competition, event):
query = "select eid from Events where ename='%s'" % (event)
cur.execute(query)
eid = cur.fetchone()[0]
query = "select cid from Competition where cname='%s'" % (competition)
cur.execute(query)
cid = cur.fetchone()[0]
delete= "delete from Competition_record where eid=%d and cid=%d and WCAid=%d" % (eid, cid, WCAid)
cur.execute(delete)
cur.commit()
def getCompetitorInfo(WCAid):
query = "select * from Competitor where WCAid=%s" % WCAid
cur.execute(query)
result = cur.fetchone()
return result
def changeCompetitorInfo(WCAid, region, name, password):
update = "update Competitor set region='%s', name='%s', password='%s' " \
"where WCAid=%s" % (region, name, password, WCAid)
print(update)
cur.execute(update)
cur.commit()
def searchCompetitor(WCAid):
query = "SELECT distinct E.ename, min(CR.avg), min(CR.best)" \
"FROM Competition_record AS CR, Events AS E " \
"WHERE CR.WCAid=%s and E.eid=CR.eid " \
"GROUP BY E.ename" % WCAid
results = []
cur.execute(query)
all = cur.fetchall()
for row in all:
l = list(row)
event = row[0]
avg = row[1]
# get the avg world rank
query = "select count(*)+1 " \
"from " \
"(select name, average from " \
"(SELECT DISTINCT C.name, min(R.avg) as average FROM Competitor AS C, Events AS E, Competition_record AS R " \
"WHERE C.WCAid=R.WCAid and E.eid=R.eid and E.ename='%s' " \
"group by C.name )" \
"order by average) T2 " \
"where T2.average<%.2f" % (event, avg)
cur.execute(query)
l.append(cur.fetchone()[0])
best = row[2]
# get the best world rank
query = "select count(*)+1 " \
"from " \
"(select * from " \
"(SELECT DISTINCT C.name, min(R.best) as best FROM Competitor AS C, Events AS E, Competition_record AS R " \
"WHERE C.WCAid=R.WCAid and E.eid=R.eid and E.ename='%s' " \
"group by C.name ) " \
"order by best) " \
"where best<%.2f" % (event, best)
cur.execute(query)
l.append(cur.fetchone()[0])
results.append(l)
# results.append(row)
return results
def getRegions():
query = "select distinct region from Competitor"
results = []
for row in cur.execute(query):
results.append(row[0])
return results
def getWorldRank(event):
results = []
# get the avg world rank
query = "select * from " \
"(select C.WCAid, C.name, min(R.avg) as average " \
"from Events E, Competition_record R, Competitor C " \
"where E.ename='%s' and E.eid=R.eid and C.WCAid=R.WCAid " \
"group by C.name, C.WCAid) " \
"order by average" % event
# print(query)
avg = []
for row in cur.execute(query):
avg.append(row)
results.append(avg)
# get the best world rank
query = "select * from " \
"(select C.WCAid, C.name, min(R.best) as best " \
"from Events E, Competition_record R, Competitor C " \
"where E.ename='%s' and E.eid=R.eid and C.WCAid=R.WCAid " \
"group by C.name, C.WCAid) " \
"order by best" % event
# print(query)
best = []
for row in cur.execute(query):
best.append(row)
results.append(best)
return results
def getRegionRank(event, region):
results = []
# get the avg region rank
query = "select * from " \
"(select C.WCAid, C.name, min(R.avg) as average " \
"from Events E, Competition_record R, Competitor C " \
"where E.ename='%s' and E.eid=R.eid and C.WCAid=R.WCAid and C.region='%s' " \
"group by C.name, C.WCAid) " \
"order by average" % (event, region)
# print(query)
avg = []
for row in cur.execute(query):
avg.append(row)
results.append(avg)
# get the best region rank
query = "select * from " \
"(select C.WCAid, C.name, min(R.best) as best " \
"from Events E, Competition_record R, Competitor C " \
"where E.ename='%s' and E.eid=R.eid and C.WCAid=R.WCAid and C.region='%s'" \
"group by C.name, C.WCAid) " \
"order by best" % (event, region)
# print(query)
best = []
for row in cur.execute(query):
best.append(row)
results.append(best)
return results
if __name__ == "__main__":
# addCompetition("1",'2','2019/11/20','2020/11/21')
# changeCompetition(4,"1",'2','2019/11/20','2020/11/21')
# print(getEvents())
# print(getCompetition())
# print(getCompetitionEvents('Nanjing Autumn 2019'))
# deleteEvent("3x3x3one-handed", "Nanjing Autumn 2019")
# print(getCompetitionRecord("3x3x3cube", "Nanjing Autumn 2019"))
# changeCompetitionRecord(5, "Nanjing Autumn 2019", "3x3x3cube", "15.78", "13.25")
# print(getCompetitorInfo("1"))
changeCompetitorInfo("1","China", "Tommy Lyman", "1")
# print(searchCompetitor("2"))
# print(getRegions())
# print(getWorldRank("3x3x3cube"))
pass