1

When I am trying to add data from SelectField to MySQL database this error is occure:

_mysql_exceptions.ProgrammingError _mysql_exceptions.ProgrammingError: not all arguments converted during string formatting

My code:

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>   

How can I solve this problem?

cPhoenix
  • 61
  • 7

1 Answers1

0

Your problem comes from

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

You should access session as

session.get('username')

Try to execute query with some string for a test to see if it will execute successfully like:

cursor.execute(query, 'myName', category, title, content))
Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57
  • 1
    I think error happens because of 'category' (SelectField) not 'session'. Because when i delete 'category' like that: cursor.execute(query, (session["username"], title, content)) it works perfectly. – cPhoenix Oct 21 '18 at 09:29
  • Can you post your html form, how you render it ? I tried you example yesterday and I think only problem is that session, because if you render all other field properly, it will contain data. – Dinko Pehar Oct 21 '18 at 10:19
  • 'code' {% from "includes/_formhelpers.html" import render_field %}
    {{ 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") }}
    – cPhoenix Oct 21 '18 at 10:52
  • Maybe you need action="" on your form. Also, please check my answer here [link](https://stackoverflow.com/questions/52899084/flask-wtforms-why-is-my-post-request-to-upload-a-file-not-sending-the-file-data/52907155#52907155) – Dinko Pehar Oct 21 '18 at 11:25