-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
170 lines (157 loc) · 5.87 KB
/
sql.py
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
import pymysql
class SQL: # 数据存储类
@staticmethod
def create_db():
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_init = "CREATE DATABASE IF NOT EXISTS mail"
cursor.execute(sql_init)
cursor.close()
conn.close()
@staticmethod
def show_tables():
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_init = "show tables from mail"
cursor.execute(sql_init)
results = cursor.fetchall()
cursor.close()
conn.close()
return results
@staticmethod
def create_sql(name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_init = "create table %s (sender char(80), receiver char(50), topic char(50), uid char(50),listnum INT, \
PRIMARY KEY(uid))" % (name)
cursor.execute(sql_init)
cursor.close()
conn.close()
@staticmethod
def search_sql(receiver, name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_search = "select * from %s WHERE receiver = '%s' " \
% (name,receiver)
cursor.execute(sql_search)
results = cursor.fetchall()
cursor.close()
conn.close()
if not results:
return
return results
@staticmethod
def search_sql_by_sender(receiver, name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_search = "select * from %s WHERE sender = '%s' " \
% (name,receiver)
cursor.execute(sql_search)
results = cursor.fetchall()
cursor.close()
conn.close()
if not results:
return
return results
@staticmethod
def search_sql_by_uid(receiver,uid, name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_search = "select * from %s WHERE receiver = '%s' AND uid='%s' " \
% (name,receiver,uid)
cursor.execute(sql_search)
results = cursor.fetchall()
cursor.close()
conn.close()
if not results:
return
return results
@staticmethod
def search_sql_by_uid_with_sender(sender,uid, name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_search = "select * from %s WHERE sender = '%s' AND uid='%s' " \
% (name,sender,uid)
cursor.execute(sql_search)
results = cursor.fetchall()
cursor.close()
conn.close()
if not results:
return
return results
@staticmethod
def delete_sql(uid, name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_delete = "DELETE FROM %s WHERE uid = '%s'" % (name,uid)
cursor.execute(sql_delete)
conn.commit()
records = cursor.fetchall()
cursor.close()
conn.close()
@staticmethod
def add_sql(sender, receiver, topic, uid,num,name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_add = "INSERT INTO %s VALUES ('%s','%s','%s','%s',%s)" % (name,sender, receiver, topic, uid,num)
try:
cursor.execute(sql_add)
except Exception as e:
print(e)
conn.commit()
cursor.close()
conn.close()
@staticmethod
def drop_table(name):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_add = "DROP TABLE %s" % (name)
cursor.execute(sql_add)
conn.commit()
cursor.close()
conn.close()
@staticmethod
def update_after_dele(index):
try:
conn = pymysql.connect(host='localhost',user='root',password='20020922WQZ',database='mail')
except Exception as e:
print(f'数据库连接失败:{e}')
cursor = conn.cursor()
sql_add = "UPDATE Mail SET listnum=listnum-1 WHERE uid IN \
(SELECT M1.uid FROM \
(SELECT M.uid FROM Mail AS M WHERE M.listnum>%d)\
AS M1);" % (index)
cursor.execute(sql_add)
conn.commit()
cursor.close()
conn.close()