我编写了以下代码,当我使用executemany时出现错误。但是当我使用循环执行查询时,它可以正常工作。我该如何解决这个问题。
def insertRows(self, query,data):
print data[0]
try:
self.cursor.executemany(query,data)
self.connection.commit()
except MySQLdb.Error, e:
try:
print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
except IndexError:
print "MySQL IndexError: %s" % str(e)
self.connection.rollback()
data[0] 的值为:
('fawn', 'clueweb09-enwp01-00-14080,clueweb09-enwp01-00-24424,clueweb09-enwp01-00-10099,clueweb09-enwp01-00-21754,clueweb09-enwp01-00-08946,clueweb09-enwp01-00-01993,clueweb09-enwp01-00-09127,clueweb09-enwp01-00-10921,clueweb09-enwp01-00-19249,clueweb09-enwp01-00-08960,clueweb09-enwp01-00-03517,,clueweb09-enwp01-00-10011,')
这是查询:
query = """
INSERT INTO tabel
(`keyValue`, `postings`)
VALUES
(%s,%s)
"""
然后,我收到以下错误:
self.cursor.executemany(query,data)
File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 255, in executemany
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
TypeError: 'NoneType' object is not iterable
此外,当我想运行以下代码时,我使用executemany命令从行中得到错误。
def insertDocId (self,docIdsMapId):
query = """
INSERT INTO DocIds
(`docName`)
VALUES
(%s)
"""
self.insertRows(query,docIdsMapId)
这是错误:
self.cursor.executemany(query,data)
File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 255, in executemany
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
TypeError: not all arguments converted during string formatting
当我使用循环而不是executemany时,它可以工作。
def insertDocId (self,docIdsMapId):
query = """
INSERT INTO DocIds
(`docName`)
VALUES
(%s)
"""
for item in docIdsMapId:
self.cursor.execute(query,[item])
self.connection.commit()