注冊程序,用戶名密碼存入數據庫


user表
表字段包含id username password error_count
注冊的程序,賬號密碼存在數據庫里面,密碼要存密文的
1.輸入input(username,password,cpwd)

2.通過select語句,判斷用戶存在不存在

3.不存在就執行insert語句
4.password加密一下

import pymysql,hashlib

def op_mysql(sql):
    db_info = {'user': 'xxx', 'password': 'xxxxxx',
            'host': '127.0.0.1', 'db': 'xxx', 'port': 3306, 'charset': 'utf8',
            'autocommit': True}
    conn = pymysql.connect(**db_info)  # 建立連接
    cur = conn.cursor(pymysql.cursors.DictCursor)  # 游標
    cur.execute(sql)  # 執行sql語句,insert 、update 、delete
    result = cur.fetchall()
    cur.close()
    conn.close()
    return result

for i in range(3):
    username = input('請輸入用戶名:').strip()
    password = input('請輸入密碼:').strip()
    cpasswd = input('請再次確認密碼:').strip()
    sql1 ='select * from user where username="%s";'%(username)
    if username=='' or password=='' or cpasswd=='':
        print('用戶名或密碼不能為空!')
    elif password != cpasswd:
        print('兩次輸入的密碼不一致,請重新注冊')
    elif  op_mysql(sql1):
        print('用戶名已存在,請重新注冊')
    else:
        ha_pwd =hashlib.md5(password.encode())
        md5_pwd = ha_pwd.hexdigest()
        sql2 = 'insert into user (username,password,error_count) values ("%s","%s",0);'%(username,md5_pwd)
        op_mysql(sql2)
        print('%s,恭喜注冊成功!' % username)
        break
else:
    print('注冊失敗,請稍后再試!')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM