[python-sybase] How to deal with DatabaseError

Greg Ward gward-pysybase at python.net
Thu Jul 20 00:11:42 EST 2006


Am I the only one who thinks that there's an ugly flaw in the Python
Database API and it's called DatabaseError?  The fact that every driver
module has to implement and export its own DatabaseError, with no
commonality between the different modules, makes life unnecessarily
difficult.  (We're planning on migrating away from Sybase 11 to something
else, quite possibly Postgres, over the next year or so ... so I cannot
afford to write piles of code that catch Sybase.DatabaseError.)

I can only see two ways to workaround this flaw:

  * write a layer that wraps the DB-API and provides its own
    DatabaseError class, which is just an alias for (e.g.)
    Sybase.DatabaseError.  My abstraction layer (so far) is a
    ConnectionFactory class, but I suspect I'll need to add more classes
    than that.  Right now, I'm starting to write code like this:

      factory = getConnectionFactory(...)
      conn = factory.connect(...)

      try:
         # do stuff with conn
      catch factory.getDatabaseError(), err:
         # handle the error

    (Hmmm: I'm also writing something to wrap the connection; maybe that's
    where the DatabaseError class should be available).

  * write a layer that wraps the DB-API and translates every (e.g.)
    Sybase.DatabaseError to its own DatabaseError.  This will make more
    sense to the Java-heads around here, but it also means that *every*
    database operation will have to go through the abstraction/translation
    layer.  Yuck.

Are there other techniques for dealing with this flaw that I haven't
thought of?

        Greg


More information about the Python-sybase mailing list