我正在尝试使用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/