0

I write the following code I got the errors when I use executemany. But when I use loop for executing query, it works properly. How Can I fixed this issue.

    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()

The value of data[0] is:

('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,')

This is the query:

query = """
    INSERT INTO tabel
    (`keyValue`, `postings`)
    VALUES
    (%s,%s)
    """

Then, I got the following error:

    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

Also when I want to run the following code I got the error from line using executemany command.

def insertDocId (self,docIdsMapId):
    query = """
    INSERT INTO DocIds
    (`docName`)
    VALUES
    (%s)
    """
    self.insertRows(query,docIdsMapId)

This is the error:

     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

When I use loop instead of executemany it works.

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

0 Answers0