mysql 登陆注册

Posted 达人就是我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 登陆注册相关的知识,希望对你有一定的参考价值。

 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 登陆注册的主要内容,如果未能解决你的问题,请参考以下文章

俺是菜鸟,俺想做一个俺android的登陆注册功能,利用socket通信,数据库是mysql。

jsp+servlet+mysql简单实现用户登陆注册

mysql 登陆注册

node+express+mysql 实现登陆注册

mysql 注册登陆表单并且操纵元素

用户登陆注册JDBC版