Skip Top Menu Navigation
Skip Top Menu Navigation

The FREE Enterprise Open Source Database

  Software Tools Interfaces Development Documentation
 

 
You Are Here:  SAP DB > 7.3 > Interfaces > sapdb (Python)

SAP DB is now SAP MaxDB! For more information, visit the SAP MaxDB pages in the SAP Developer Network.

SAP DB: Module sapdb for Python

Module Contents LONG values Installation

Examples

Exceptions

Module Contents

class SapDB_Session

Allows the execution of SQL statements.
When the last reference to the session is dropped, the database connection is released with an implicit transaction rollback.

sql
sqlResult = session.sql (cmd)

Executes an SQL statement.
Returns data values on SELECT INTO, a SapDB_ResultSet on SELECT and otherwise an empty list.

sqlX
sqlResult = session.sqlX (cmd [, parms])

Executes an SQL statement with arguments.
Returns data values on SELECT INTO, a SapDB_ResultSet on SELECT and otherwise an empty list.

prepare
session.prepare (cmd)

Parses an SQL statement and creates an SapDB_Prepared object.

release
session.release ( [withCommit])

Closes the connection. The default is to end the session without COMMIT.

commit
session.commit ()

Commits the current transaction.

rollback
session.rollback ()

Aborts the current transaction.

class SapDB_ResultSet

Allows navigation and the retrieval of a result set.
Objects are created by all SQL statements that create a result set (SapDB_Session::sql, SapDB_Prepared::execute).

A SapDB_ResultSet can be used as an iterator:
for row in result set:
    ... process row ...
This will iterate over the result set from the beginning, even if some rows have been fetched.

next
result = resultSet.next ()

Gets the next row.
Output values are returned as a list. The end of the result set is indicated by None.

previous
result = resultSet.previous ()

Gets the previous row.
Output values are returned as a list. The end of the result set is indicated by None.

relative
result = resultSet.relative (count)

Moves the cursor relatively to the current row.
A negative value of count will move the cursor backwards in the result set. Output values are returned as a list. The end of the result set is indicated by None.

absolute
result = resultSet.absolute (i)

Gets the row no. i.
Output values are returned as a list. The first row has the index 1. The end of the result set is indicated by None.

first
result = resultSet.first ()

Gets the first row.
Output values are returned as a list. The end of the result set is indicated by 1.

last
result = resultSet.last ()

Gets the last row.
Output values are returned as a list. The end of the result set is indicated by 1.

current
result = resultSet.current ()

Returns the current row.
Output values are returned as a list.

cursorName
result = resultSet.cursorName ()

Returns the name of the result set.

columnCount
result = resultSet.columnCount ()

Returns the number of columns.

columnInfo
result = resultSet.columnInfo (i)

Returns information about a column specifying four values:
  • column name
  • column type
  • length of the column
  • number of fractional digits
The first column has the index 0.
getDescription
result = resultSet.getDescription ()

Returns information about the output columns.
A tuple is returned. Each tuple element consists of a column description. A column description is a tuple with the following elements:
  • column name
  • column type as a string
  • column type as an ANSI/ODBC type integer
  • length of the column
  • number of fractional digits
  • specification whether the column may contain NULL
  • specification whether the column is IN, OUT or IN/OUT (always OUT)
Example:
descriptions = resultSet.getDescription ()
for name, typeName, typeCode, colLength, colPrecision, \
        isNullable, inout in descriptions:
    ...

class SapDB_LongReader

Allows LONGs to be retrieved.

This is a callable object. Calling it with an integer argument will return the next bytes of the LONG (similar to calling the read method).

read
result = reader.read (count)

Returns the subsequent count characters.
Returns an empty string when the end of the LONG is reached. Currently, parameter count is not optional (unlike the read method of file objects).

class SapDB_Prepared

Allows the execution of SQL statements with parameters.
Objects are created with SapDB_Session::prepare.

execute
result = prepared.execute ( [sqlParms])

Executes statements with arguments.
Output values are returned as a list. If the statement creates a result set, then a SapDB_ResultSet is returned. The arguments must be specified as a list.

getDescription
result = prepared.getDescription ()

Returns information about the parameters. A tuple is returned. Each tuple element consists of a parameter description. A parameter description is a tuple with the following elements:
  • parameter name (currently the empty string)
  • parameter type as a string
  • parameter type as an ANSI/ODBC type integer
  • length of the parameter
  • number of fractional digits
  • specification whether the parameter may contain NULL
  • specification whether the parameter is IN, OUT or IN/OUT
Example:
descriptions = resultSet.getDescription ()
for name, typeName, typeCode, colLength, colPrecision, \
        isNullable, inout in descriptions:
    ...

connect

session = sapdb.connect (user, pwd, dbname [, host [, config]])

Establishes the connection to the database.
The username is case sensitive. config is a string of the following format:
keyword=value[&keyword=value]...
Allowed keywords are:
  • sqlmode (adabas, oracle, sapr3, ansi, db2)
  • isolation (isolation level as an integer)
  • timeout (command timeout in seconds)

_buildInfo

result = sapdb._buildInfo ()

Returns a string containing the version and build number.

LONG values

Input

Input values for LONGs can be either a string or a callable object with the following properties:
  • The object is called with an integer argument, number of bytes requested
  • The object must return a string
  • The end of the stream is indicated by an empty string

Output

Output values of LONGs are of the type SapDB-LongReader and can be read using the method read.

Installation

Copy the file <dependent_path>/misc/sapdbmodule.so (in Windows NT: sapdb.pyd) to a directory that can be found by the Python interpreter, e.g. a directory in PYTHONPATH.

Examples

How to test SQL features: egSQL.py

How to test inserts of LONGs and SELECT them: longtest.py

Exceptions

  • CommunicationError:

    The communication link to the server failed. The exception value is an instance with the following attributes:
    • errorCode
    • message
  • SQLError:

    The execution of the SQL statement resulted in an error. The exception value is an instance with the following attributes:
    • errorCode
    • message
    • errorPos
    • sqlState


Search
See also ...












Questions or comments about the Web site? Contact the webmaster.