我想使用python将多个记录插入到Mysql中。
我尝试过类似的事情:
if request.method == 'POST':
req_json = request.get_json(force=True)
dir(req_json)
#sys.exit()
g.cursor.execute("INSERT INTO gmr (version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10})",
(req_json[0]['version'], req_json[0]['visitorId'], req_json[0]['dwellTime'], req_json[0]['poiId'], req_json[0]['srId'], req_json[0]['zoneId'], req_json[0]['poiProximityConfidence'], req_json[0]['zoneProximityConfidence'], req_json[0]['poiPresenceConfidence'], req_json[0]['zonePresenceConfidence'], req_json[0]['normalizedTime']))
我传递json对象,如:
[
{
"dwellTime":"2",
"normalizedTime":"1489574977000",
"poiId":"poi3",
"poiPresenceConfidence":"15",
"poiProximityConfidence":"1",
"srId":"sr3",
"version":"15",
"visitorId":"V123",
"zoneId":"Security",
"zonePresenceConfidence":"40",
"zoneProximityConfidence":"20"
},
{
"dwellTime":"234234",
"normalizedTime":"1489574977000",
"poiId":"poi3",
"poiPresenceConfidence":"15",
"poiProximityConfidence":"1",
"srId":"sr3",
"version":"16",
"visitorId":"V123",
"zoneId":"Security",
"zonePresenceConfidence":"40",
"zoneProximityConfidence":"20"
}
]
我得到的错误如下:
ProgrammingError:并非在字符串格式化期间转换所有参数
Traceback (most recent call last): File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1997, in call return self.wsgi_app(environ, start_response)
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app response = self.handle_exception(e)
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb)
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request()
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e)
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request()
File "/root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint
File "/root/python-mysql/venv/py_gmr.py", line 56, in add (req_json[0]['version'], req_json[0]['visitorId'], req_json[0]['dwellTime'], req_json[0]['poiId'], req_json[0]['srId'], req_json[0]['zoneId'], req_json[0]['poiProximityConfidence'], req_json[0]['zoneProximityConfidence'], req_json[0]['poiPresenceConfidence'], req_json[0]['zonePresenceConfidence'], req_json[0]['normalizedTime']))
File "/root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 283, in executemany self.rowcount = sum(self.execute(query, arg) for arg in args)
File "/root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 283, in self.rowcount = sum(self.execute(query, arg) for arg in args)
File "/root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 240, in execute self.errorhandler(self, ProgrammingError, str(m))
File "/root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/connections.py", line 52, in defaulterrorhandler raise errorclass(errorvalue) ProgrammingError: not all arguments converted during string formatting
如果我将{0}, {1}
替换为%s
,则只插入一个值
答案 0 :(得分:0)
您不能在SQL语句中使用{}
作为占位符。您必须使用%s
。
使用{}
无法修复您的问题"只插入一组值。这是预期的,因为您只访问JSON中的第一组值。如果要同时插入它们,则需要使用for循环迭代覆盖值并每次插入一组 - 就像尝试对任何列表执行任何操作一样。