-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation_db.py
170 lines (152 loc) · 4.49 KB
/
operation_db.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
import hashlib
import warnings
import time
from e_dictionary.settings import *
warnings.filterwarnings('ignore')
class DataBase:
"""
电子辞典数据库操作
"""
def __init__(self):
self.db = pymysql.connect(
HOST,
USERNAME,
PASSWORD,
Database,
charset='utf8'
)
def create_cursor(self):
"""
创建游标
:return:
"""
self.cur = self.db.cursor()
self.cur.execute(c_db) # 创建数据库
self.cur.execute(u_db) # 进入数据库
self.cur.execute(ut_db) # 创建user表
self.cur.execute(ht_db) # 创建history表
def registered(self, username, password, tell):
"""
注册
:param username:
:param password:
:param tell:
:return:
"""
tell = self.md5(username, tell)
password = self.md5(username, password)
sql = "select * from user where username='%s';" % username
self.cur.execute(sql)
if self.cur.fetchone():
return False
sql = "insert into user(username, password, tell) values (%s, %s, %s);"
try:
self.cur.execute(sql, [username, password, tell])
self.db.commit()
except Exception as e:
self.db.rollback()
print(e)
return False
return True
def login(self, username, password):
"""
登录验证
:param username:
:param password:
:return:
"""
password = self.md5(username, password)
sql = "select * from user where username='%s';" % username
self.cur.execute(sql)
lo = self.cur.fetchone()
if lo:
sql = "select * from user where username='%s' and password='%s';" % (username, password)
self.cur.execute(sql)
re = self.cur.fetchone()
if re:
return 'ok'
return '账号或密码错误'
return '账号不存在'
def close(self):
"""
关闭
:return:
"""
self.cur.close()
self.db.close()
def md5(self, username, data):
"""
MD5加密
:param password:
:return:
"""
md5 = hashlib.md5((username + '!@#$').encode())
md5.update(data.encode())
return md5.hexdigest()
def rest(self, username, tell):
"""
重置验证
:param username:
:param tell:
:return:
"""
sql = "select * from user where username='%s' and tell='%s'" % (username, self.md5(username, tell))
self.cur.execute(sql)
r = self.cur.fetchone()
if r:
return True
else:
return False
def updata(self, username, tell, psd):
"""
重置密码
:param username:
:param tell:
:param psd:
:return:
"""
sql = "update user set password='%s' where username='%s' and tell='%s';" % (self.md5(username, psd), username, self.md5(username, tell))
try:
self.cur.execute(sql)
self.db.commit()
except Exception as e:
self.db.rollback()
print(e)
return False
return True
def insert_history(self, username, word):
"""
插入查询记录
:param username:
:param word:
:return:
"""
tm = time.ctime()
print(tm)
sql = "insert into history(username, word, time) values (%s, %s, %s);"
try:
self.cur.execute(sql, [username, word, tm])
self.db.commit()
except Exception as e:
self.db.rollback()
print(e)
def find_word(self, word):
"""
查询单词
:param word:
:return:
"""
sql = "select * from word_list where word='%s';" % word
self.cur.execute(sql)
mean = self.cur.fetchone()
return mean
def find_history(self, username):
"""
查询历史记录(最近10条)
:param username:
:return:
"""
sql = "select username, word, time from history where username='%s' order by id desc limit 10;" % username
self.cur.execute(sql)
return self.cur.fetchmany(10)