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

Adam Hooper adamh at densi.com
Wed Nov 9 00:06:03 EST 2005


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>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://www.object-craft.com.au/pipermail/python-sybase/attachments/20051108/885aa4ff/attachment.pgp


More information about the Python-sybase mailing list