[英]flask-mysql ProgrammingError: not all arguments converted during string formatting
I am trying to make a simple program using flask + mysql. 我正在尝试使用flask + mysql创建一个简单的程序。 I just receive two arguments in a POST and write to the database: 我只是在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)
The problem is that when i try to post the data from the client , i am getting: 问题是,当我尝试从客户端发布数据时,我得到:
ProgrammingError: not all arguments converted during string formatting
127.0.0.1 - - [28/Apr/2018 23:46:14] "POST /upd HTTP/1.1" 500 -
If i comment the SQL statement, the client will receive the output of jsonify which looks correct: 如果我注释SQL语句,则客户端将收到看起来正确的jsonify输出:
{
"lat": "3.2001",
"lng": "-11.45465"
}
You need to specify variables using %s or %(name)s parameter style, according to the docs : 根据文档 ,您需要使用%s或%(name)s参数样式指定变量:
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). 使用%s或%(name)s参数样式(即,使用format或pyformat样式)指定变量。
cur.execute('insert into locations(lat,lng) values (%s,%s)' % (_lat,_lng,))
should work. 应该管用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.