gpt4 book ai didi

Python Flask - WTF SelectField 数据导致错误 _mysql_exceptions.ProgrammingError

转载 作者:行者123 更新时间:2023-11-30 21:38:21 24 4
gpt4 key购买 nike

当我试图将数据从 SelectField 添加到 MySQL 数据库时,出现了这个错误:

_mysql_exceptions.ProgrammingError_mysql_exceptions.ProgrammingError: 并非所有参数都在字符串格式化期间转换

我的代码:

class ContentForm(Form):
category = SelectField("Category: ",choices=[('DataScience','Data Science'), ('ComputerProgramming', 'Computer Programming'),('DigitalMarketing','Digital Marketing'),('Other','Other')], validators=[validators.DataRequired()])
title = StringField("Title: ",validators=[validators.length(min= 5, max = 100), validators.DataRequired()])
content = TextAreaField("Content: ", validators=[validators.length(min = 10), validators.DataRequired()])

@app.route("/addcontent", methods=["GET","POST"])
@login_required
def addcontent():
form = ContentForm(request.form)
if request.method == "POST" and form.validate():
category = form.category.data
title = form.title.data
content = form.content.data
cursor = mysql.connection.cursor()
query = "INSERT INTO contents (author, category, title,content) VALUES(%s,%s,%s)"
cursor.execute(query, (session["username"], category, title, content))
mysql.connection.commit()
cursor.close()
flash("Your content have added successfully. Thanks your contributions.", 'success')
return redirect(url_for("dashboard"))
return render_template("addcontent.html", form = form)

HTML:

{% from "includes/_formhelpers.html" import render_field %}

<form method="post">

{{ render_field(form.category, class="form-control", style = "width: 40% !important") }}
{{ render_field(form.title, class="form-control", style = "width: 40% !important") }}
{{ render_field(form.content, class="form-control", style = "height: 300px !important") }}

<button type="submit" class="btn btn-primary btn-lg">Add Content</button>

我该如何解决这个问题?

最佳答案

你的问题来自

cursor.execute(query, (session["username"], category, title, content))

您应该访问 session

session.get('username')

尝试使用一些字符串执行查询以进行测试,看看它是否会成功执行,如下所示:

cursor.execute(query, 'myName', category, title, content))

关于Python Flask - WTF SelectField 数据导致错误 _mysql_exceptions.ProgrammingError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com