为什么MySql只保存字符串的第一个字符? (我使用的是python 3)

时间:2018-04-28 17:11:03

标签: mysql python-3.x flask insert

我试图在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()

1 个答案:

答案 0 :(得分:0)

这是解决方案:

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