-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsql.py
More file actions
372 lines (339 loc) · 10.5 KB
/
sql.py
File metadata and controls
372 lines (339 loc) · 10.5 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import pymysql
def open():
# 打开数据库连接
db = pymysql.connect("localhost", "root", "Yzf123456", "sushe")
return db
def checkX(SID,Lno,Sno,MID,Mname,new):
#检查学生正确性
flag = True
msg = ''
db = open()
cursor = db.cursor()
sql1 = "select MID,Mname from M_table where MID = {}".format(MID)
sql2 = "select SID from X_table where SID = {}".format(SID)
sql3 = "select C_n from S_table where Lno = {} and Sno = {}".format(Lno,Sno)
cursor.execute(sql1)
results1 = cursor.fetchall()
cursor.execute(sql2)
results2 = cursor.fetchall()
cursor.execute(sql3)
results3 = cursor.fetchall()
if results1 == ():
msg += '没有该宿管、'
flag =False
elif results1[0][1] != Mname:
msg += '宿管名字错误、'
flag = False
if results3 == ():
msg += "没有该宿舍、"
flag = False
if results3 != () and results3[0] ==0:
msg += "该宿舍已满、"
flag = False
if results2 != () and new:
msg += "学号重复、"
flag = False
db.close()
return (flag,msg)
def student_add(student):
#增加学生
db = open()
cursor = db.cursor()
sql1 = """insert into x_table(SID,Sname,Ssex,Lno,Sno,MID,Mname)
values ("{}","{}",{},"{}","{}","{}","{}")""".format(student.SID,student.Sname,student.Ssex,student.Lno,student.Sno,student.MID,student.Mname)
sql2 ="update s_table set C_n = C_n-1,L_n = L_n+1 where Lno = {} and Sno = {}".format(student.Lno,student.Sno)
try:
# 执行SQL语句
cursor.execute(sql1)
cursor.execute(sql2)
except Exception as e:
db.rollback() # 事务回滚
print('增加学生失败', e)
else:
db.commit() # 事务提交
print('增加学生成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def student_delete(student):
db = open()
cursor = db.cursor()
sql1 = "delete from x_table where SID = {}".format(student.SID)
sql2 ="update s_table set C_n = C_n+1,L_n = L_n-1 where Lno = {} and Sno = {}".format(student.Lno,student.Sno)
try:
# 执行SQL语句
cursor.execute(sql1)
cursor.execute(sql2)
except Exception as e:
db.rollback() # 事务回滚
print('删除学生失败', e)
else:
db.commit() # 事务提交
print('删除学生成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def student_select(seachby,keyList):
#学生单属性查询
db = open()
cursor = db.cursor()
sql1 = "select * from x_table where {} REGEXP '{}' ".format(seachby,keyList)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询学生失败', e)
else:
results = cursor.fetchall()
print('查询学生成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results
def student_multiselect(seachby,keyList):
#学生多属性查询
db = open()
cursor = db.cursor()
sql1 = "select * from x_table "
for i in range(len(seachby)):
if i == 0:
sql1 = sql1 + "where {} REGEXP '{}' ".format(seachby[i],keyList[i])
else:
sql1 = sql1 + "and {} REGEXP '{}' ".format(seachby[i], keyList[i])
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询学生失败', e)
else:
results = cursor.fetchall()
print('查询学生成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results
def Load(table):
#加载table表的全部信息
db = open()
cursor = db.cursor()
sql = "select * from {}".format(table)
# 执行SQL语句
cursor.execute(sql)
results = cursor.fetchall()
# 关闭数据库连接
db.close()
return results
def checkM(MID,new):
#检查宿管正确性
flag = True
msg = ''
db = open()
cursor = db.cursor()
sql = "select MID from M_table where MID = {}".format(MID)
cursor.execute(sql)
results1 = cursor.fetchall()
if results1 != () and new:
msg += '已有该宿管'
flag =False
db.close()
return (flag,msg)
def manager_add(manager):
#增加宿管
db = open()
cursor = db.cursor()
sql1 = """insert into m_table(MID,Mname,Msex,Mage,Mphone)
values ("{}","{}",{},"{}","{}")""".format(manager.MID,manager.Mname,manager.Msex,manager.Mage,manager.Mphone)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('增加宿管失败', e)
else:
db.commit() # 事务提交
print('增加宿管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def manager_edit(manager):
#更改宿管
db = open()
cursor = db.cursor()
sql1 = """update m_table set Mname = "{}",Msex= {},Mage = {},Mphone = "{}" where MID = "{}" """.format(manager.Mname,manager.Msex,manager.Mage,manager.Mphone,manager.MID)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('更改宿管失败', e)
else:
db.commit() # 事务提交
print('更改宿管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def manager_delete(manager):
db = open()
cursor = db.cursor()
sql1 = "delete from m_table where MID = {}".format(manager.MID)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('删除宿管失败', e)
else:
db.commit() # 事务提交
print('删除宿管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def manager_select(seachby,keyList):
#宿管单属性查询
db = open()
cursor = db.cursor()
sql1 = "select * from m_table where {} REGEXP '{}' ".format(seachby,keyList)
print(sql1)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询宿管失败', e)
else:
results = cursor.fetchall()
print('查询宿管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results
def manager_multiselect(seachby,keyList):
#宿管多属性查询
db = open()
cursor = db.cursor()
sql1 = "select * from m_table "
for i in range(len(seachby)):
if i == 0:
sql1 = sql1 + "where {} REGEXP '{}' ".format(seachby[i],keyList[i])
else:
sql1 = sql1 + "and {} REGEXP '{}' ".format(seachby[i], keyList[i])
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询宿管失败', e)
else:
results = cursor.fetchall()
print('查询宿管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results
def checkS(Lno,Sno,new):
#检查宿舍正确性
flag = True
msg = ''
db = open()
cursor = db.cursor()
sql1 = "select C_n,L_n,K_n from S_table where Lno = {} and Sno = {}".format(Lno,Sno)
cursor.execute(sql1)
results1 = cursor.fetchall()
print(results1)
if results1 != () and new:
msg += "已有该宿舍、"
flag = False
if not new:
if results1[0][0] + results1[0][1] != results1[0][2]:
msg += "人数不对、"
flag = False
db.close()
return (flag,msg)
def sushe_add(sushe):
#增加宿舍
db = open()
cursor = db.cursor()
sql1 = """insert into s_table(Lno,Sno,L_n,C_n,K_n,Location)
values ({},{},{},{},{},"{}")""".format(sushe.Lno,sushe.Sno,sushe.L_n,sushe.C_n,sushe.K_n,sushe.Location)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('增加宿舍失败', e)
else:
db.commit() # 事务提交
print('增加宿舍成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def sushe_edit(sushe):
#更改宿舍
db = open()
cursor = db.cursor()
sql1 = """update s_table set L_n = {},C_n = {},K_n = {},Location = "{}" where Lno = {} and Sno = {} """.format(sushe.L_n,sushe.C_n,sushe.K_n,sushe.Location,sushe.Lno,sushe.Sno)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('更改宿舍失败', e)
else:
db.commit() # 事务提交
print('更改宿舍成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def sushe_delete(sushe):
db = open()
cursor = db.cursor()
sql1 = "delete from s_table where Lno = {} and Sno={}".format(sushe.Lno,sushe.Sno)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
db.rollback() # 事务回滚
print('删除宿舍失败', e)
else:
db.commit() # 事务提交
print('删除宿舍成功', cursor.rowcount)
# 关闭数据库连接
db.close()
def sushe_multiselect(seachby,keyList):
#宿舍多属性查询
db = open()
cursor = db.cursor()
sql1 = "select * from s_table "
for i in range(len(seachby)):
if i == 0:
sql1 = sql1 + "where {} REGEXP '{}' ".format(seachby[i],keyList[i])
else:
sql1 = sql1 + "and {} REGEXP '{}' ".format(seachby[i], keyList[i])
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询宿舍失败', e)
else:
results = cursor.fetchall()
print('查宿舍管成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results
def sushe_select(seachby,keyList):
#宿舍单属性查询
db = open()
cursor = db.cursor()
#print(seachby)
#print(keyList)
sql1 = "select * from s_table where {} REGEXP '{}' ".format(seachby,keyList)
try:
# 执行SQL语句
cursor.execute(sql1)
except Exception as e:
results = ()
db.rollback() # 事务回滚
print('查询宿舍失败', e)
else:
results = cursor.fetchall()
print('查询宿舍成功', cursor.rowcount)
# 关闭数据库连接
db.close()
return results