python - Flask-mysql 编程错误 : not all arguments converted during string formatting

标签 python mysql flask

我正在尝试使用flask + mysql制作一个简单的程序。 我只是在 POST 中收到两个参数并写入数据库:

@app.route('/upd',methods=['POST'])
def upd():
   _lat = request.form['lat']
   _lng = request.form['lng']

   cur = mysql.connection.cursor()
   cur.execute('insert into locations(lat,lng) values (?,?)',(_lat,_lng,))
   cur.commit()

   return jsonify(request.form)

问题是,当我尝试从客户端发布数据时,我得到:

ProgrammingError: not all arguments converted during string formatting
127.0.0.1 - - [28/Apr/2018 23:46:14] "POST /upd HTTP/1.1" 500 -

如果我注释 SQL 语句,客户端将收到看起来正确的 jsonify 输出:

{
"lat": "3.2001",
"lng": "-11.45465"
}

最佳答案

您需要使用 %s 或 %(name)s 参数样式指定变量,according to the docs :

The parameters found in the tuple or dictionary params are bound to the variables in the operation. Specify variables using %s or %(name)s parameter style (that is, using format or pyformat style).

cur.execute('插入位置(lat,lng)值(%s,%s)' % (_lat,_lng,))

应该可以。

关于python - Flask-mysql 编程错误 : not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50082951/

相关文章:

python - 雅虎体育 API XML 命名空间找不到元素

python - 如何在不先将文件保存为 png 或 jpeg 的情况下将 numpy 数组传递给 openCV?

mysql - 配置 MySQL 服务器以避免 "MySQL Server has gone away"错误

python - Flask + WTForms + SelectMultipleField 和动态选择

python - AWS lambda 克隆 git 存储库并使 zip 无法在 s3 中上传

php - 在 laravel 中加入查询

mysql - 第 2 行存在语法错误 (1064)

python - 类型错误 : __init__() takes 1 positional argument but 3 were given

python - 每个 Flask session 存储大数据或服务连接

python - 如何对分类列和数值列进行分组,并基于该组对数值进行分箱