[python-sybase] How could I get the database message returned from Sybase in python script?

Wang King wangkknd at gmail.com
Wed Nov 9 12:36:43 EST 2005


Thanks a lot.It works well. :-)


2005/11/8, Adam Hooper <adamh at densi.com>:
> On Tue, 2005-11-08 at 17:18 +0800, Wang King wrote:
> > HI,everyone:
> > I'd install the  Sybase Module for python successful,but I don't know
> > how to get the database message returned from Sybase.For example,I
> > execute sql "SELECT * FROM TABLE_NOEXIST",I want to get the message
> > from database like "TABLE_NOEXIST not found...."but not
> > "Sybase.DatabaseError".
> >
> > I'm  jackeroo for python.So please give an example for this problem.
>
> That "Sybase.DatabaseError" is an exception; it can be caught and dealt
> with. I think you'll find that whenever the table doesn't exist you'll
> get the same "Msg" name; so you can parse the error string and handle
> the specific case when the the table does not exist. It'll look like
> this:
>
> try:
>        c.execute(sql)
> except Sybase.DatabaseError, e:
>        # See if the error is "table does not exist"
>        # change 5555 to whichever number that's supposed to be
>        if "Msg 5555" in str(e):
>                do_something()
>        else:
>                # If it's another error, pretend we didn't catch it
>                raise
>
> As you can see, the message is found using "str(e)" in the "except"
> block. The *actual* string returned by the server is on the second line
> -- in str(e).split('\n')[1]. But unless you're writing a generic SQL
> client, you'd be far better off looking at the message numbers as I did
> above than looking at the string returned by the server. (For instance,
> the server's returned string might be French for some servers, but the
> number will always be the same.)
>
> Good luck!
>
> --
> Adam Hooper <adamh at densi.com>
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQBDcKK790GtBxQ61zARAi8zAKDlzJv1s6PevHWrtugiRGfKCKLhpgCgzOOk
> L768oq6KyTLJ6roZmhMJNK0=
> =OuaU
> -----END PGP SIGNATURE-----
>
>
>


More information about the Python-sybase mailing list