Sdkpython
Sdkpython
Required Components
Access to an Adaptive Server database using the Python programming language requires
these components:
sybpydb extension module for the Python scripting language.
Open Client SDK provides application development tools that allow access to data
source, information application or system service.
DBCAPI is a function library that acts as intermediate conversion layer between the
extension module and the CT-Library.
CT-Library (CT-Lib API) is part of the Open Client suite. CT-Library sends commands
to Adaptive Server and processes results.
Version Requirements
For information about platform support, see the Software Developers Kit and Open Server
Installation Guide for your platform.
Adaptive Server Enterprise version 15.7 or later
Python installation version 2.6 built-in threaded mode.
Open Client SDK version 15.7 or later.
Programmers Guide 1
Adaptive Server Enterprise Extension Module for Python
type you choose is Typical or Full. For complete installation and configuration instructions,
see the Software Developers Kit and Open Server Installation Guide for your platform.
Configuration Tasks
Configuration task include:
Python module search path
The name and address of the target server
Security and Directory Services
Runtime configuration through the ocs.cfg file
$SYBASE\$SYBASE_OCS 2.7
\python\py-
thon27_64\dll
$SYBASE\$SYBASE_OCS 3.1
\python\py-
thon31_64\dll
$SYBASE/$SYBASE_OCS/ 3.1
python/python31_64r/
lib
Runtime Configuration
Use the runtime configuration file ocs.cfg to set:
Property values
Server option values
Server capabilities
Debugging options
See Using the Open Client and Open Server Runtime Configuration File in the Open Client
Client-Library/C Reference Manual for information about the file syntax and the properties
that can be set in the file.
Programmers Guide 3
Adaptive Server Enterprise Extension Module for Python
# Create a connection
conn = sybpydb.connect(user='john', password='sybase')
#Create a connection.
conn = sybpydb.connect(user='sa')
To specify output parameters, the extension module provides the OutParam constructor. This
support is an extension to the Python DBAPI specification. The callproc() method returns a
list of all the parameters passed to the method. If there are output parameters, and no result sets
generated from the store procedure, the list contains the modified output values as soon as
callproc() completes. However, if there are result sets, the list does not contain modified
Programmers Guide 5
Adaptive Server Enterprise Extension Module for Python
output values until all result sets from the stored procedure have been retrieved using the
fetch*() methods and a call to nextset() is made to check if there are any more result sets. The
nextset() method must be called even if only one result set is expected.
More examples of different output parameter types are available in the sample program
callproc.py.
dec_in = decimal.Decimal('1.23')
dec_out = sybpydb.OutParam(decimal.Decimal('0.00'))
dec_in = decimal.Decimal('1.23')
dec_out = sybpydb.OutParam(decimal.Decimal('0.00'))
m_in = decimal.Decimal('9.87')
m_out = sybpydb.OutParam(decimal.Decimal('0.00'))
vals = cur.callproc('pyproc', (int_out, dec_in, dec_out, m_in,
m_out))
Support for date, time, datetime, and float parameters for stored procedures
The Adaptive Server Enterprise extension module for Python supports for date, time,
datetime, and float parameters for stored procedures.
See callproc.py sample that demonstrates calling stored procedures with parameters of
different datatypes including date, time, datetime, float, and integer. The sample also
demonstrates the handling of output parameters.
Programmers Guide 7
Adaptive Server Enterprise Extension Module for Python
apilevel
String constant stating the supported DB API level. The default value is 2.0.
class sybpydb.apilevel
paramstyle
String constant stating the type of parameter marker formatting expected by the interface. The
value for this constant is qmark. The interface expects question mark style parameter
formatting, for example:
'...WHERE name=?'
class sybpydb.paramstyle
threadsafety
Integer constant stating the level of thread safety that the interface supports. The threadsafety
constant value for the module is 1, which indicates that the module can be shared but not the
connections.
class sybpydb.threadsafety
close()
Closes the connection to the server. The connection is unusable after the call, and raises an
exception if any operation is attempted. The same applies to cursor objects attempting to
access the connection.
connection.close()
commit()
rollback()
cursor()
connection.cursor()
messages()
This is a Python list object to which the module appends tuples (exception class and exception
object) for all messages that the module receives for this connection. An error on any cursor
obtained from the same connection object, is appended to the messages attribute of the
connection object of the cursor.
connection.messages()
Usage example:
try:
cur.execute("select ...")
except sybpydb.Error:
for err in cur.connection.messages:
print("Exception %s, Value %s", % err[0], err[1])
callproc
Calls a stored database procedure with the given name. After fetching all the result sets and
rows, use the proc_status attribute to check the status result of the stored procedure.
cursor.callproc()
execute
executemany
Prepares a database operation and executes it against all parameter sequences found in the
sequence seq_of_parameters.
cursor.executemany()
fetch
Fetches the next row of a query result set, returning a single sequence, or None when no more
data is available.
cursor.fetch()
fetchmany
Fetches the next set of rows of a query result, returning a sequence of sequences, for example,
a list of tuples. An empty sequence is returned when no rows are available.
Programmers Guide 9
Adaptive Server Enterprise Extension Module for Python
cursor.fetchmany()
fetchall
Fetches all (remaining) rows of a query result, returning them as a sequence of sequences.
cursor.fetchall()
description
nextset
Forces the cursor to skip to the next available set, discarding any remaining rows from the
current set.
cursor.nextset()
arraysize
This read/write attribute specifies the number of rows to fetch at a time with fetchmany(). It
defaults to 1 which indicates to fetch a single row at a time.
cursor.arraysize()
proc_status
Read/write attribute specifies the number of rows to fetch at a time with fetchmany(). Defaults
to 1 which indicates to fetch a single row at a time.
cursor.proc_status
Error
Exception that is the base class of all other exceptions defined by sybpydb. Error is a subclass
of the Python StandardError exception.
sybpydb.Error
InterfaceError
Exception raised for errors that are related to the database interface rather than the database
itself. It is a subclass of Error.
sybpydb.InterfaceError
DatabaseError
Exception raised for errors that are related to the database. It is a subclass of Error.
sybpydb.DatabaseError
DataError
Exception raised for errors that are related to problems with the processed data. It is a subclass
of DatabaseError.
sybpydb.DataError
OperationalError
Exception raised for errors that are related to problems to the operation of the database but are
not necessarily under the control of the programmer. It is a subclass of DatabaseError.
sybpydb.OperationalError
IntegrityError
Exception raised when the relational integrity of the database is affected. It is a subclass of
DatabaseError.
sybpydb.IntegrityError
InternalError
ProgrammingError
NotSupportedError
Programmers Guide 11
Adaptive Server Enterprise Extension Module for Python
Glossary
Glossary of term specific to scripting languages.
Client-Library part of Open Client, a collection of routines for use in writing client
applications. Client-Library is designed to accommodate cursors and other advanced
features in the Sybase product line.
CS-Library included with both the Open Client and Open Server products, a collection
of utility routines that are useful to both Client-Library and Server-Library applications.
CT-Library (CT-Lib API) is part of the Open Client suite and is required to let an
scripting application connect to Adaptive Server.
DBD database vendor-specific-driver that translates DBI database API calls into a form
that is understood by the target database SDK.
Extension or module the Python language can be extended by modules that are written
in Python.
Python is an interpreted, general-purpose high-level programming language. For more
information, go to http://www.python.org.
thread a path of execution through Open Server application and library code and the
paths associated stack space, state information, and event handlers.
Transact-SQL an enhanced version of the database language SQL. Applications can use
Transact-SQL to communicate with Adaptive Server Enterprise.
Programmers Guide 13
Glossary
Index
C
components I
description 1
input parameters 5
required 1
installation options 1
compute rows processing 6
configuration
runtime 2 O
search path 2
security and directory services 2 output parameters 5
target server 2
task 2 P
connection
closing 4 parameter support
establishing 4 dynamic statements 6
cursor object 4 stored procedures 6
parameters
D date 6
datetime 6
data decimal 6
accessing 4 float 6
updating 4 money 6
data flow diagram 1 time 6
E S
extension module stored procedures 5
connection object methods 8 sybpydb interface 4
cursor object methods 9
interface constants 8
interface methods 7 V
loading 4
Python API Reference 7 version requirements 1
warning and error messages 10
W
G
warning and error messages 10
Glossary 13
Programmers Guide 15
Index