0

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の代わりに loop を使用すると機能します。

def insertDocId (self,docIdsMapId):
    query = """
    INSERT INTO DocIds
    (`docName`)
    VALUES
    (%s)
    """
    for item in docIdsMapId:
        self.cursor.execute(query,[item])
        self.connection.commit()
4

0 に答える 0