python-mysql用户登录

import pymysql
from hashlib import sha1
config={
    'host':"localhost",
    'port':3306,
    'user':"root",
    'passwd':"HelloWorld",
    'db':"lianxi",
    'charset':"utf8"
}
# 接收用户输入
while True:
    try:
        conn = pymysql.connect(**config)
        cus1 = conn.cursor()
    except Exception:
        print("数据库连接异常,请检查数据库连接配置")
        break
    name = input("请输入用户名:")
    pwd = input("请输入密码:")
    pwd=sha1(pwd.encode('utf-8')).hexdigest()
    # 根据用户名查询密码
    sql = 'select password from test where name=%s'
    cus1.execute(sql,name)
    pwddata = cus1.fetchall()
    if pwddata == ():
        print("用户名或密码错误")
    elif pwddata[0][0] == pwd:
        print("登陆成功")
        conn.close()
        break
    else:
        print("用户名或密码错误")

你可能感兴趣的:(python-mysql用户登录)