-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdbconn.py
225 lines (185 loc) · 6.35 KB
/
dbconn.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
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
'''
Descripttion:
version:
Author: Catop
Date: 2021-02-10 09:10:27
LastEditTime: 2021-02-14 23:24:05
'''
#coding:utf-8
import pymysql
'''连接数据库配置'''
conn = pymysql.connect(host='',user = "qqbot",passwd = "",db = "qqbot")
def check_cmd(user_id):
#获取最近一条命令
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#结果返回dict
sql = f"SELECT last_cmd FROM userinfo WHERE user_id={user_id} LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
last_cmd = cursor.fetchone()['last_cmd']
conn.commit()
return last_cmd
def check_register(user_id):
#检查用户是否注册
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT uid FROM userinfo WHERE user_id={user_id} LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
user_info = cursor.fetchall()
conn.commit()
if(len(user_info)>=1):
return 1
else:
return 0
def insert_img(user_id,file_name,upload_date,upload_time,ocr_err_code,ocr_times,ocr_scores):
params = [file_name,user_id,upload_date,upload_time,ocr_err_code,ocr_times,ocr_scores]
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"INSERT INTO imginfo(file_name,user_id,upload_date,upload_time,ocr_err_code,ocr_times,ocr_scores) VALUES(%s,%s,%s,%s,%s,%s,%s)"
conn.ping(reconnect=True)
cursor.execute(sql,params)
conn.commit()
return
def get_user(user_id):
#获取用户姓名和班级
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT user_name,user_class FROM userinfo WHERE user_id={user_id} LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
user_info = cursor.fetchone()
conn.commit()
return user_info
def register_user(user_id,user_name,user_class):
#注册新用户
try:
params = [user_id,user_name,user_class]
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"INSERT INTO userinfo(user_id,user_name,user_class) VALUES(%s,%s,%s)"
conn.ping(reconnect=True)
cursor.execute(sql,params)
conn.commit()
except:
flag=0
else:
flag=1
return flag
def re_register_user(user_id,user_name,user_class):
#重新注册,更新用户信息
try:
params = [user_name,user_class,user_id]
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"UPDATE userinfo SET user_name=%s,user_class=%s WHERE user_id=%s"
conn.ping(reconnect=True)
cursor.execute(sql,params)
conn.commit()
except:
flag=0
else:
flag=1
return flag
def check_today_upload(user_id,upload_date):
#检查用户当日是否已经上传过
user_id = str(user_id)
upload_date = str(upload_date)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT imgid FROM imginfo WHERE(user_id={user_id} AND upload_date='{upload_date}')"
conn.ping(reconnect=True)
cursor.execute(sql)
if(len(cursor.fetchall())>=1):
return 1
conn.commit()
else:
return 0
conn.commit()
def check_status(user_id):
#返回指定用户最新一条记录的时间
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT upload_date FROM imginfo WHERE user_id={user_id} ORDER BY upload_date DESC LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
try:
last_date = cursor.fetchone()['upload_date']
conn.commit()
except TypeError:
last_date = '1970-01-01'
return last_date
def get_class_members(user_class):
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
params = [user_class]
sql = f"SELECT user_id FROM userinfo WHERE user_class=%s"
conn.ping(reconnect=True)
cursor.execute(sql,params)
sql_ret = cursor.fetchall()
conn.commit()
class_menbers = []
for i in range(0,len(sql_ret)):
class_menbers.append(sql_ret[i]['user_id'])
return class_menbers
def get_latest_img_info(user_id,upload_date):
"""获取指定用户指定时间最新照片信息"""
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
params = [user_id,upload_date]
sql = f"SELECT * FROM imginfo WHERE (user_id=%s AND upload_date=%s) ORDER BY imgid DESC LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql,params)
sql_ret = cursor.fetchall()
conn.commit()
return sql_ret
def manual_update(ocr_times,ocr_socres,imgid):
#更新手动核对信息
try:
params = [ocr_times,ocr_socres,imgid]
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"UPDATE imginfo SET ocr_err_code=-1,ocr_times=%s,ocr_scores=%s WHERE imgid=%s"
conn.ping(reconnect=True)
cursor.execute(sql,params)
conn.commit()
except:
flag=0
else:
flag=1
return flag
def err_check(imgid):
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT ocr_err_code FROM imginfo WHERE imgid={imgid} ORDER BY upload_date DESC LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
try:
ocr_err_code = cursor.fetchone()['ocr_err_code']
conn.commit()
except:
flag = 0
else:
flag = 1
return ocr_err_code
def get_user_by_migid(imgid):
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql = f"SELECT user_id FROM imginfo WHERE imgid={imgid} ORDER BY upload_date DESC LIMIT 1"
conn.ping(reconnect=True)
cursor.execute(sql)
try:
user_id = cursor.fetchone()['user_id']
conn.commit()
except:
flag = 0
else:
flag = 1
cursor1 = conn.cursor(cursor=pymysql.cursors.DictCursor)
sql1 = f"SELECT user_name FROM userinfo WHERE user_id={user_id} ORDER BY uid DESC LIMIT 1"
conn.ping(reconnect=True)
cursor1.execute(sql1)
try:
user_name = cursor1.fetchone()['user_name']
conn.commit()
except:
flag = 0
else:
flag = 1
return user_name
if __name__=='__main__':
#print(get_user(601179193))
#insert_img('601179193','test.jpg','2021-02-10','09:47:49')
#print(check_today_upload('601179193','2021-02-10'))
#print(register_user('29242764','李四','信安20-1'))
#print(check_status(601179193))
#print(get_class_members('信安20-2'))
print(get_latest_img_info('601179193','2021-02-13'))