码迷,mamicode.com
首页 > 数据库 > 详细

mysql 登陆注册

时间:2019-01-05 16:34:27      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:com   root   col   登录   cut   class   word   127.0.0.1   .exe   

 1 import pymysql, hashlib
 2 class loginRegistration:
 3     def __init__(self):
 4         # 连接数据库
 5         self.bankData = pymysql.connect("127.0.0.1", "root", "123123", "user")
 6         # 设置字符编码
 7         self.bankData.set_charset(utf8)
 8         # 创建游标对象
 9         self.cursor = self.bankData.cursor()
10 
11     def registration(self):
12     # 查找login表所有数据
13         sql = select * from login
14         # 运行sql句子
15         self.cursor.execute(sql)
16         # 如果数据为0
17         if self.cursor.rowcount == 0:
18             print(注册用户为0)
19         else:
20             print(注册人数为, self.cursor.rowcount, "")
21         userInputName = input(请输入用户名)
22         userInputPassword = input(请输入密码)
23         sql = select username from login where username="{}".format(userInputName)
24         self.cursor.execute(sql)
25         # 如果数据为0
26         if self.cursor.rowcount == 0:
27             # 调用md5加密
28             passwordMd5 = hashlib.md5()
29             passwordMd5.update(userInputPassword.encode(utf8))
30             password = passwordMd5.hexdigest()
31             # 添加数据,用try来检测提交是否成功
32             try:
33                 sql = "insert into login(username,password) values(‘{}‘,‘{}‘ )".format(userInputName, password)
34                 self.cursor.execute(sql)
35                 # 提交服务器中
36                 self.bankData.commit()
37                 print("注册成功")
38             except:
39                 print("注册失败")    
40         else:
41             print("用户已存在")
42 
43     def login(self):
44         sql = select * from login
45         # 运行sql句子
46         self.cursor.execute(sql)
47         userInputName = input(请输入用户名)
48         userInputPassword = input(请输入密码)
49         sql = select username,password from login where username="{}".format(userInputName)
50         self.cursor.execute(sql)
51         passwordMd5 = hashlib.md5()
52         passwordMd5.update(userInputPassword.encode(utf8))
53         password = passwordMd5.hexdigest()
54         # 如果数据为0
55         if self.cursor.rowcount == 0:
56             print("用户不已存在")
57         elif self.cursor.fetchone()[1] != password:
58             print("密码错误")
59         else:
60             print("登录成功")
61 
62 
63 lr = loginRegistration()
64 while True:
65     print("1、注册  2、登录")
66     userinput = input("")
67     if userinput is "1":
68         lr.registration()
69     elif userinput is "2":
70         lr.login()

 

mysql 登陆注册

标签:com   root   col   登录   cut   class   word   127.0.0.1   .exe   

原文地址:https://www.cnblogs.com/liyaobin/p/10224792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!