#python#mysql注册模块出现‘name ‘uname’ is not defined```’报错(未解决)

#python mysql注册模块
import pymysql
import hashlib

def uimg(uname,upwd_sha1):
    uname = input('请输入用户名:')
    upwd = input('请输入密码:')

    s1 = hashlib.sha1()
    s1.update(upwd.encode())
    upwd_sha1 = s1.hexdigest()
    
    return uname,upwd_sha1

def mysql_register(upwd_sha1,uname):

    try:
        uimg(uname, upwd_sha1)

        conn = pymysql.connect(host="localhost",port="3306",user="root",password="5200",database="userimg",charset="utf8")
        cur = conn.cursor()
        sql = 'select count(*) from userimg where uname=%s'
        cur.execute(sql, [uname])
        res = cur.fetchone()

        if res[0] == 1:
            print('用户名已经存在,注册失败,mysql')
        else:
            sql = 'insert into img(name,pwd) VALUES (%s,%s)'
            res = cur.execute(sql,[uname,upwd_sha1])
            conn.commit()
            if res == 1:
                print('注册成功,mysql')
            else:
                print('注册失败,数据库插入失败,mysql')

    except Exception as e:
        print(f'注册失败,抛出异常{e},mysql')

    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
        mysql_register(uname,upwd_sha1)


  1. 系统会一直报错:
    Traceback (most recent call last):
    File “E:/Pythontest/sql.py”, line 43, in
    mysql_register(uname,upwd_sha1)
    NameError: name ‘uname’ is not defined```

我查了百度,大多是说没有导入模块,该导的我都导了。
假设,不是采用调用uimg()的方式,而是把语句全放在mysql_register()里就不会出现报错,能正常使用。

2.解决方法:
??
暂时没有找到……

`


你可能感兴趣的:(python-数据库)