为什么mysql只保存字符串的第一个字符(我正在使用python 3)

lpwwtiir  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我试图保存在mysql数据库中随机生成的代码,但它只保存第一个字符,我尝试了不同的方法,但没有工作。这是我的密码:

try:
    error = None
    with mysql.connection.cursor() as cursor:
        passwordd = ''
        if request.method == 'GET' or 'POST':
            for n in range(0, 8):
                x = random.randint(0, 9)
                if x <= 3:
                    passwordd += str(random.choice('abcdefghijklmnopqrstuvxyz'))
                elif x <= 6:
                    passwordd += str(random.choice('abcffsddddd'))
                else:
                    passwordd += str(random.randrange(0, 9))
                    return passwordd
                sql = "INSERT INTO generatedcode(password) VALUES (%s)"
                cursor.execute(sql, (passwordd))
                mysql.connection.commit()
qxsslcnc

qxsslcnc1#

以下是解决方法:

passwordd = ''

    if request.method == 'GET' or 'POST':

        for n in range(0, 8):

            x = random.randint(0, 9)
            if x <= 3:
                passwordd += str(random.choice('abcdefghijklmnopqrstuvxyz'))
            elif x <= 6:
                passwordd += str(random.choice(',.;:-_*%&/()=?!'))
            else:
                passwordd += str(random.randrange(0, 9))

        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO generatedcode(password) VALUES (%s)", [passwordd])
        mysql.connection.commit()
        cur.close()
        return passwordd

相关问题