|
From: Adam H. <ad...@de...> - 2005-11-09 08:05:03
|
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".
>=20
> 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!
--=20
Adam Hooper <ad...@de...>
|