我试图在 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()
最佳答案
解决方案如下:
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
关于mysql - 为什么MySql只保存字符串的第一个字符? (我使用的是Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50079088/