原本把注册的代码也写了出来的 但是执行一次成功后就不行了 原因我也没搞懂(ps:刚入坑的小程序员) 求大佬指点呀 print("Thank You!")
#-*-coding:utf-8 -*- import pymysql import getpass#导入密码密文包 def login(): print("Please enter UserName and PassWord!") db=pymysql.connect("localhost","root","weixiao","login")#连接数据库 while True: username=input("USERNAME:") password=getpass.getpass("PASSWORD:")#隐藏回显密码,输入的时候不会显示输入的字符和长度 cursor=db.cursor()#使用 cursor创建游标对象 s="select * from user where username='%s'" % (username)#查询数据库中该用户的用户名对应一栏 cursor.execute(s)#执行MySQL语句 results=cursor.fetchall()#Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。 for row in results:#遍历查询的结果 usn=row[0]#把第一个值(username)赋值给usn pwd=row[1]#把第二个值(password)赋值给pwd if username==usn and password==pwd:#判断用户名和密码是否正确 print('Login Success!!!!') db.close()#关闭数据库连接 return False#返回False结束循环 print('The Key or User is Wrong!Try again~')#用户名或者密码不正确则再次循环重新输入用户名及密码 if __name__=='__main__': login()数据库的代码应该是正确的吧 不然第一条也插不进去呀,明明两次输入的密码都是一致的 但是就卡在密码那循环 唉!先记录下再慢慢找原因
下面是注册的代码:
import pymysql import getpass#导入密码密文包 def register(): print("welcom join us ^_^") db=pymysql.connect("localhost","root","weixiao","login") username=input("UserName:") while True: password=getpass.getpass("Password:") password1=getpass.getpass("Enter Password Again:") if password is not password1:#如果两次输入的密码不一致则一直循环 print("The passwords entered did not match!Please Reinput!") else: sql="insert into register(username,password)\ value('%s','%s')" %(username,password) cursor=db.cursor() try: cursor.execute(sql) db.commit()#执行MySQL语句 print("Register is Success *_*") return False except: print("Register is fail T_T") db.rollback()#插入失败则回滚数据 db.close() if __name__=='__main__': register()