[DB-SIG] General DB Error Handling

Robert Theiss rtheiss@yahoo.com
Wed, 26 Jun 2002 08:37:37 -0700 (PDT)


All,

I am writing a python script to connect to an informix
database using informixdb.py.  I am new to python and
object oriented programmign in general, but I have
good experience using embedded SQL and C . 

What I would like to do is trap any error returned
from the database, so I can exit the program
gracefully.  A sample output is  shown below, when I
intentionally generate an SQL error.  

[bobt@monza:/service/bobt] > python InfTest.py
WARNING: Python C API version mismatch for module
_informixdb:
  This Python has API version 1010, module _informixdb
has version 1007.
WARNING: Python C API version mismatch for module dbi:
  This Python has API version 1010, module dbi has
version 1007.
20020626
Traceback (most recent call last):
  File "InfTest.py", line 69, in ?
    db.execute(SqlSelectStatement)
  File
"/opt/python/lib/python2.1/site-packages/informixdb.py",
line 73, in execute
    return apply(self._cursor.execute, args)
InformixdbError: Error -522 performing PREPARE: Table
(part) not selected in query.
[bobt@monza:/service/bobt] > 

The python code that generated this output is shown
below:

LocNbr = 96
SqlSelectStatement = """
    select inv_audit.loc_nbr,
extend(inv_audit.txn_datet, year to day),  
           part.part_brand, part.part_nbr,
part.part_desc,
           part.dealer_cost,  inv_audit.avg_cost,     
   
           inv_audit.qty_txn,
inv_audit.user_id_created_by, 
           inv_audit.inv_audit_info,
inv_audit.txn_code, inv_audit.part_id
    from
           inv_audit         
    where
           inv_audit.txn_type = 'RC' and
	   inv_audit.txn_code = 'RE' and
           inv_audit.loc_nbr =  %i and 
	   inv_audit.part_id = part.part_id;
     """ % LocNbr 
db.execute('set isolation dirty read;')
# db.execute(SqlSelectStatement)
try:
  returncode = apply(db.execute(SqlSelectStatement),
args)
except:
  print "return code is %s : " %  args

I'm sure this is basic python, but I've been banging
my head against this wall for two days.  This is
simply my latest attempt. 
What I am looking for is a way to trap/report the
informix -522 error that is shown above when the
python module aborted.   

Any help is appreciated.  

Regards,

Bob Theiss
Programmer/Analyst

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com