How to find bad row with db api executemany()?

Miki Tebeka miki.tebeka at gmail.com
Sat Mar 30 12:24:31 EDT 2013


> I can catch the exception, but don't see any way to tell which row caused the problem.  Is this information obtainable, short of retrying each row one by one?
One way to debug this is to wrap the iterable passed to executemany with one that remembers the last line. Something like:

    class LastIterator(object):
        def __init__(self, coll):
            self.it = iter(coll)
            self.last = None

        def __iter__(self):
            return self

        def next(self):
            self.last = next(self.it)
            return self.last

      ...
      li = ListIterator(items)
      try:
           cursor.executemany(sql, li)
      except SQLError, e:
           print('Error: {}, row was {}'.format(e, li.last))



More information about the Python-list mailing list