原本把註冊的代碼也寫了出來的 但是執行一次成功後就不行了 原因我也沒搞懂(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()