【问题标题】:MySQLInterfaceError: Python type list cannot be convertedMySQLInterfaceError:无法转换 Python 类型列表
【发布时间】:2020-07-23 00:37:54
【问题描述】:
    @app.route('/upload', methods=["GET", "POST"])
def upload():
    if request.method == 'POST':

        file = request.files['upFile']
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file.filename))

        dir = app.config['UPLOAD_FOLDER']+ file.filename

        cmd  = "INSERT INTO posts (title, author, file, date, subject, field ) VALUES (%s, %s, %s, %s, %s, %s)"
        post = (request.form["title"], session['user_name'], dir, x, request.form["subject"], request.form.getlist("field[]"))
        crsr.execute(cmd, post)
        mydb.commit()

        return redirect(url_for('upload'))
    else:
        return render_template('upload.htm')

它返回:MySQLInterfaceError: Python 类型列表无法转换

【问题讨论】:

  • 欢迎来到 Stackoverflow!为了帮助其他人更好地解决您的问题,请在您的帖子中分享有关您的问题的更多信息,代码 sn-ps 是让其他支持者更清楚地了解可能是什么问题的最佳方式
  • 这将返回一个列表:`request.form.getlist("field[]"). Why are you retrieving a list here? What is the datatype of the field` 列在数据库表中?

标签: python mysql python-3.x flask mysql-python


【解决方案1】:

一种解决方案是像这样将列表转换为字符串。

','.join(list)

【讨论】:

  • 这很容易被sql注入吧?
猜你喜欢
  • 2021-12-02
  • 2021-12-31
  • 2021-12-24
  • 1970-01-01
  • 2021-11-14
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多