我正在尝试用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"
}发布于 2018-04-29 11:10:55
您需要使用%s或%(name)s参数样式指定变量,according to the docs:
在元组或字典参数中找到的参数被绑定到操作中的变量。使用%s或%(name)s参数样式(即使用format或pyformat样式)指定变量。
cur.execute('insert into locations(lat,lng) values (%s,%s)' % (_lat,_lng,))
应该行得通。
https://stackoverflow.com/questions/50082951
复制相似问题