我试图使用Flask制作一个表单,并将表单的数据上传到MySQL数据库,但我遇到了这个错误。
1064 (42000): 你的SQL语法有错误,请检查你的MySQL服务器版本对应的手册,以了解在第1行'%s)'附近使用的正确语法。
我使用了以下代码。
if request.method=="POST" and form.validate():
username=form.username.data
email=form.email.data
password=sha256_crypt.encrypt((str(form.password.data)))
c,conn=connections()
x = c.execute("SELECT * FROM users WHERE username = (%s)",
(thwart(username)))
if int(x) > 0:
flash("That username is already taken, please choose another")
return render_template('register.html', form=form)
else:
c.execute("INSERT INTO users (username, password, email, tracking) VALUES (%s, %s, %s, %s)",
(thwart(username), thwart(password), thwart(email), thwart("/introduction-to-python-programming/")))
conn.commit()
flash("Thanks for registering!")
c.close()
conn.close()
你没有设置什么 %s
错了,应该是这样
x = c.execute("SELECT * FROM users WHERE username = (%s)" %
(thwart(username)))
或
x = c.execute(f"SELECT * FROM users WHERE username = {thwart(username))}")