[Tutor] odbc exception handling

Chris Reay Chris.Reay@pareto-partners.com.au
Thu, 6 May 1999 03:33:17 +0100


Hi

I'd like to know how to handle exceptions in the odbc module in PythonWin.
This code ...

class SentBrokerForecastMgr(BrokerForecastMgr):
    
    def add(self, aSentFc):
        tup = aSentFc.asTuple()
        sqlStr = self.insertStr % (tup[0], tup[1], tup[2], tup[3])
        self.cursor.execute(sqlStr)
        self.dbc.commit()
        
correctly raises the  exception

dbi.integrity-error: [Microsoft][ODBC Microsoft Access 97 Driver] The
changes you requested to the table were not successful because they would
create duplicate values in the index, primary key, or relationship.  Change
the data in the field or fields that contain duplicate data, remove the
index, or redefine the index to permit duplicate entries and try again. in
EXEC

when it strikes an integrity error.

I can't seem to trap the exception. I try (unintended pun)

    def add(self, aSentFc):
        tup = aSentFc.asTuple()
        sqlStr = self.insertStr % (tup[0], tup[1], tup[2], tup[3])
        try:
            self.cursor.execute(sqlStr)
            self.dbc.commit()
        except dbi.integrity-error:
            print "Integrity error"

which yields

Traceback (innermost last):
  File "<interactive input>", line 0, in ?
  File "FcScripts.py", line 73, in loadWeeklySentimentBrokerForecasts
    sm.add(eachFc)
  File "S:\chrisr\PyPrograms\BrokerForecastMgr.py", line 74, in add
    except dbi.integrity-error:
AttributeError: integrity

I also try

    def add(self, aSentFc):
        tup = aSentFc.asTuple()
        sqlStr = self.insertStr % (tup[0], tup[1], tup[2], tup[3])
        try:
            self.cursor.execute(sqlStr)
            self.dbc.commit()
        except StandardError:
            print "error"

but this doesn't trap the error.

What am I doing wrong?

Thanks

Chris

---------------------------------------------------------------
Chris Reay
Pareto Partners Australia Pty Ltd
Level 49 Governor Phillip Tower
1 Farrer Place
Sydney
NSW 2000

Phone:	(02) 9247 3700 (w)
Email:	chris.reay@pareto-partners.com.au
---------------------------------------------------------------
Warning: The author is not responsible.
---------------------------------------------------------------