-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.py
78 lines (65 loc) · 1.97 KB
/
exercise.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
"""
pymysql 数据库操作流程
"""
import pymysql
class Databases:
def __init__(self):
# 连接数据库
self.db = pymysql.connect(host='localhost',
port=3306,
user='root',
password='123456',
database='stu',
charset='utf8')
# 创建游标对象(操作数据库语句,获取查询结果)
self.cur = self.db.cursor()
def close(self):
# 关闭游标
self.cur.close()
# 断开数据库连接
self.db.close()
def register(self, name, passwd):
sql = "select * from user where name='%s'" % name
self.cur.execute(sql)
resule = self.cur.fetchone()
if resule:
return False
try:
sql = "insert into user (name,passwd) values (%s,%s)"
self.cur.execute(sql, [name, passwd])
self.db.commit()
return True
except:
self.db.rollback()
return False
def login(self, name, passwd):
sql = "select * from user where name='%s' and passwd='%s'" % (name, passwd)
self.cur.execute(sql)
result = self.cur.fetchone()
if result:
return True
else:
return False
if __name__ == '__main__':
db = Databases()
while True:
print("""
============
1.注册 2.登录
============
""")
cmd = input("命令:")
if cmd == '1':
if db.register('张三', '123'):
print("注册成功")
break
else:
print("注册失败")
elif cmd == '2':
if db.login('张三', '123'):
print("登录成功")
break
else:
print("登录失败")
else:
print("请输入正确指令!")