简体   繁体   English

flask-mysql编程错误:字符串格式化期间并非所有参数都已转换

[英]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.

相关问题 ProgrammingError:并非所有参数都在字符串格式化期间转换 - ProgrammingError: not all arguments converted during string formatting Flask,并非在字符串格式化期间转换所有参数 - Flask, not all arguments converted during string formatting Scrapy MySQL: MySQLdb._exceptions.ProgrammingError: 并非所有 arguments 在字节格式化期间转换 - Scrapy MySQL: MySQLdb._exceptions.ProgrammingError: not all arguments converted during bytes formatting TypeError:并非在字符串格式化过程中转换了所有参数[MySQL DB连接] - TypeError: not all arguments converted during string formatting [MySQL DB connection] TypeError:并非使用MySQL和Python在字符串格式化期间转换所有参数 - TypeError: not all arguments converted during string formatting using MySQL and Python MySQL + Python:在字符串格式化期间并非所有参数都被转换 - MySQL + Python: Not all arguments converted during string formatting MySQL数据库调用:在字符串格式化期间,并非所有参数都已转换 - MySQL db call: not all arguments converted during string formatting 并非在select的字符串格式化期间转换了所有参数 - not all arguments converted during string formatting for select TypeError:在字符串格式化期间并非所有参数都已转换 - TypeError: not all arguments converted during string formatting 类型错误:并非所有参数都在字符串格式化期间转换 - TypeError: not all arguments converted during string formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM