美文网首页
python-mysql用户登录

python-mysql用户登录

作者: 夜雨_87aa | 来源:发表于2018-08-19 21:10 被阅读0次
    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用户登录

          本文链接:https://www.haomeiwen.com/subject/sgrhiftx.html