Python Sybase Module

Sadly, this module is not compliant with the db-sig's API. If I were to rewrite it from scratch, it would be but, well, there you go.

The python Sybase module defines the following:

sybase.new()

Constructor for the database connection class. This function takes three arguments:
userSybase Userid.
passwdSybase password for this user.
serverThe database server required. This string will be looked up in the Sybase interfaces file.

Any of these arguments may be None. If user or passwd are None, then the respective parameter is not set for the Sybase login. If server is None, a default server is used.

This function returns a newly allocated object of type sybdb, which contains the state of the connection to the desired database, and methods for using that connection.

sybase.error

This is the exception raised whenever Sybase reports an error. The associated value for the exception is a tuple (ErrorNumber, ErrorMessage), where ErrorNumber is the Sybase error number, and ErrorMessage is the Sybase error string.

Type sybdb

SybDBtype is the type of the class where most of the work of this module is performed. The class has the following attributes:

sql()

This method takes one argument, a string containing TSQL commands to be passed to Sybase. The return value of this function is a list, each element of which is the result of a TSQL command. i.e. if the string passed contains only one command, the result is a list with one item, the result of the query. Note: it is possible to perform multiple queries with one call to this function. Each query result (in the case of a SELECT query) is a list of rows, each row being a tuple of the columns returned.

This would probably be made clearer by an example:

import sybase

sdb=sybase.new('sa', 'sapassword', 'DBNAME')

sdb.sql('USE master')

res=sdb.sql('SELECT * FROM sysdevices')
for row in res[0]:
	for column in row:
		# Some action here...

This code fragment sends two command to the Sybase server, the first, a USE command, returns nothing (sql() will return the empty list []), and the second, a SELECT statement, will return a singleton list of a list of tuples.

The module can return the following Sybase types (as the python type):

Note: the module can send any type, as the data sent to the SQL server is a string, but the data received must be converted into a native python type.
Note: NULL columns are represented as the python object None.

sp()

This method executes a stored procedure on the Sybase server. It takes two arguments, a string and a tuple. The tuple may be made up only of integers, floats and strings. The second argument may also be None if it is not required. The return value is a tuple of an integer return status and a result (same format as returned by sql()).

Compiling and installing the sybase module

The sybase module requires the Sybase Open Client/C libraries and header files to be present on your system. You will also require a correctly configured interfaces file. You should probably ensure that you can use other Open Client applications (like isql) before you proceed (Note: a correctly functioning isql does not indicate the presence of the Open Client/C libraries). You may be able to use the freeware Linux Open Client/C libraries, available from ftp://ftp.sybase.com/pub/linux, although this has not been tested. You will also require a Sybase server, and a valid username/password combination in order to use the module for anything productive (Well obviously:)

To compile the sybase module, the following entry to Modules/Setup may be employed:

sybasemodule.c -I$(SYBASE) -L$(SYBASE) -lsybdb

Note that your operating system may require extra libraries to be present to correctly link the module. If you are having compilation problems due to unresovable symbols, this should be your first suspicion. Digital Unix requires the addition of -ldnet_stub (or -ldnet if you want to use DECnet). Check your Sybase Open Client/C documentation for further details.

The module may be compiled to use dynamic loading if your operating system supports it. It should (theoretically) be possible to compile and use the module under MSDOS, Microsoft Windows, Macintosh and Unix. The module is known to function under Unix (Sun and DEC).

Credits

The original module was written by John Redford

It was then hacked on by Tim Docker and Thomas Palmer, before being ripped to shreds and re-assembled by Bernard Gardner.

This documentation written by bernardg@fsis.usyd.edu.au (bernardg@g7.org).

The following is the original Sybase.README from before Bernard started
messing around:
                      [README from ./Sybase.shar]

From timd@phm.gov.auMon Aug 19 14:05:25 1996
Date: Mon, 19 Aug 1996 09:12:27 +1000
From: Tim Docker 
To: Peter John Godman 
Cc: python-list@cwi.nl
Subject: sybase module


Peter John Godman writes:
 > Hi folks.  I've been trying without much success to locate the most recent 
 > sybase module for python.  The one I have, which I got from python.org,
 > does not support Datetime types, cursors, or NULLs!  
 > 
 > I read in the documentation that there is an updated on distributed with 
 > the Julian package, but it's not there.

Some months ago I patched a sybase module that I found, sufficently to
satisfy my needs. I haven't got the code here though. I fixed some
bugs, added support for Dates and NULL columns, but never considered
cursors.

It sure ain't meant for the mainstream, but mail me if you want it,
and I'll try and dig it up.

Tim

------------------------------------------------
Tim Docker
Powerhouse Museum
Sydney, Australia
                          Phone: ++61 2 217 0232
timd@phm.gov.au           Fax:   ++61 2 217 0433
------------------------------------------------

Date:    Wed, 06 Mar 1996 11:22:58 -0500
From:    thomas@webserver.rave.net
To:      guido@CNRI.Reston.VA.US
Subject: **** Sybase module Changes ****

I have made several changes to the sybasemodule.

1.) added sybase datetime conversion.
2.) fixed single column select.  - returned tulpe (col,)  which caused several
    problems when trying to unpack the tuple.  
3.) added sybase err_handler, msg_handlers.
    If sybase returns a error, a tuple is build and an exception is raised.

Please review my changes, this is only my third module and I am still working
on symatics.  

I posted a message to the news group about my changes, and received several
requests to e-mail my source to them???  I did'nt!  someone needs to handle
the source - why have several copies running around.

Thanks!
Thomas Palmer
Sr. Sybase DBA/Systems Programmer.
To which I'd add:
I did a lot of work on the module, while working for the FSIS project at the University of Sydney. I tracked and fixed a large number of bugs, and made the sql() method pretty close to stable. The rp() method needs a similar amount of work. I'd love to do it, but my contract that involved using Python with Sybase has just expired, and it's unlikely that I'll get to finish the work.

Bernard Gardner
Thu Feb 27 16:24:47 EST 1997