pymysql實現註冊登入
阿新 • • 發佈:2020-12-22
import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123456', database='day48', charset='utf8' ) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) while True: msg = ''' ======= 歡迎來到註冊登入介面 ======= | 1.註冊 | | 2.登入 | | 3.退出 | ============== END ============== ''' print(msg) func_num = input("請輸入功能編號:").strip() if func_num == '1': while True: reg_usr = input('請輸入使用者名稱:').strip() reg_pwd1 = input("請輸入密碼:").strip() reg_pwd2 = input("請確認密碼:").strip() if not (reg_usr and reg_pwd1 and reg_pwd2): print('輸入不能為空') continue if not reg_pwd1 == reg_pwd2: print('兩次密碼不一致') continue sql = "select * from user where usr=%s;" if cursor.execute(sql, (reg_usr,)): print('使用者名稱已存在') continue sql = 'insert into user(usr,pwd) values(%s, %s)' cursor.execute(sql, (reg_usr, reg_pwd1)) conn.commit() # 提交資料到資料庫並儲存 print('註冊成功') break elif func_num == '2': while True: log_usr = input('請輸入賬號:').strip() log_pwd = input("請輸入密碼:").strip() if not (log_usr and log_pwd): print('賬號密碼不能為空') continue sql = "select * from user where usr=%s and pwd=%s;" if cursor.execute(sql, (log_usr, log_pwd)): print('登入成功!') break print('賬號或密碼錯誤!') elif func_num == '3': exit() else: print('輸入錯誤,該命令不存在!')