使用executemany命令时出现不同的错误

时间:2017-06-06 21:29:41

标签: python-2.7 mysql-python executemany

我在编写以下代码时使用 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()

0 个答案:

没有答案