Python:使用executemany()将多个记录插入mysql

时间:2017-04-04 15:34:05

标签: python mysql

我试图通过pyhton将json数据发布到mysql中:

更新

@app.route('/gmradd', methods=['POST'])
def add():
    if request.method == 'POST':
        req_json = request.get_json(force=True)
        g.cursor.execute('INSERT INTO gmr (version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',
        (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']))

        g.conn.commit()
        resp = Response('Updated', status=201, mimetype='application/json')
        return resp
    else:
        return """<html><body>
        Something went horribly wrong
        </body></html>"""

我在休息客户端应用程序中发布了2条json数据记录,如下所示:

[
{
"dwellTime": "0",
"normalizedTime": "1489574977000",
"poiId": "poi3",
"poiPresenceConfidence": "15",
"poiProximityConfidence": "1",
"srId": "sr3",
"version": "3",
"visitorId": "V123",
"zoneId": "Security",
"zonePresenceConfidence": "40",
"zoneProximityConfidence": "20"
},
{
"dwellTime": "0",
"normalizedTime": "1489574977000",
"poiId": "poi3",
"poiPresenceConfidence": "15",
"poiProximityConfidence": "1",
"srId": "sr3",
"version": "3",
"visitorId": "V123",
"zoneId": "Security",
"zonePresenceConfidence": "40",
"zoneProximityConfidence": "20"
},
]

只保存第一个值!

尝试使用executemany()并将%s替换为{0}&#39;,&#39; {1}&#39;,...赞:

g.cursor.executemany("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']))

不使用&#39; {0}&#39;&#39; {1}如果我使用%s,%s,..

我得到:TypeError:格式字符串

的参数不够
  

追踪(最近一次通话):    文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;, 1997年,致电       return self.wsgi_app(environ,start_response)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;,第1985行,在wsgi_app中       response = self.handle_exception(e)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1540行,在handle_exception中       重新加注(exc_type,exc_value,tb)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;,第1982行,在wsgi_app中       response = self.full_dispatch_request()

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1614行,在full_dispatch_request中       rv = self.handle_user_exception(e)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1517行,在handle_user_exception中       重新加注(exc_type,exc_value,tb)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1612行,在full_dispatch_request中       rv = self.dispatch_request()

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1598行,在dispatch_request中       return self.view_functionsrule.endpoint

     

文件&#34; /root/python-mysql/venv/py_gmr.py",第56行,另外       (req_json [0] [&#39;版本&#39;],req_json [0] [&#39; visitorId&#39;],req_json [0] [&#39;停留时间&#39;],req_json [0] [&#39; poiId&#39;],req_json [0] [&#39; srId&#39;],req_json [0] [&#39; zoneId&#39;],req_json [0] [&#39; poiProximityConfidence&#39;],req_json [0] [&#39; zoneProximityConfidence&#39;],req_json [0] [&#39; poiPresenceConfidence&#39;],req_json [0] [&#39; zonePresenceConfidence&#39;] ,req_json [0] [&#39; normalizedTime&#39;]))

     

File&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py" ;,第281行,在executemany       self._get_db()。编码)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py",第297行,在_do_execute_many中       v =值%escape(next(args),conn)   TypeError:格式字符串

的参数不足

使用&#39; {0}&#39;,&#39; {1}:

追溯是:

  

追踪(最近一次通话):     文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;, 1997年,致电       return self.wsgi_app(environ,start_response)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;,第1985行,在wsgi_app中       response = self.handle_exception(e)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1540行,在handle_exception中       重新加注(exc_type,exc_value,tb)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py" ;,第1982行,在wsgi_app中       response = self.full_dispatch_request()

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1614行,在full_dispatch_request中       rv = self.handle_user_exception(e)

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1517行,在handle_user_exception中       重新加注(exc_type,exc_value,tb)    文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1612行,在full_dispatch_request中       rv = self.dispatch_request()

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/flask/app.py",第1598行,在dispatch_request中       return self.view_functionsrule.endpoint

     

文件&#34; /root/python-mysql/venv/py_gmr.py",第56行,另外       (req_json [0] [&#39;版本&#39;],req_json [0] [&#39; visitorId&#39;],req_json [0] [&#39;停留时间&#39;],req_json [0] [&#39; poiId&#39;],req_json [0] [&#39; srId&#39;],req_json [0] [&#39; zoneId&#39;],req_json [0] [&#39; poiProximityConfidence&#39;],req_json [0] [&#39; zoneProximityConfidence&#39;],req_json [0] [&#39; poiPresenceConfidence&#39;],req_json [0] [&#39; zonePresenceConfidence&#39;] ,req_json [0] [&#39; normalizedTime&#39;]))

     

File&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py" ;,第283行,在executemany       self.rowcount = sum(args中arg的self.execute(query,arg))

     

File&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py" ;,第283行,在       self.rowcount = sum(args中arg的self.execute(query,arg))

     

File&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/cursors.py" ;,第240行,执行       self.errorhandler(self,ProgrammingError,str(m))

     

文件&#34; /root/python-mysql/venv/lib/python2.7/site-packages/MySQLdb/connections.py",第52行,在defaulterrorhandler中       提出错误类(错误值)   ProgrammingError:并非在字符串格式化期间转换所有参数

0 个答案:

没有答案