Executing SQL Statements and Querying Information About the Result Set

In this example you use the methods sql and getDescription of the sdb.sql module. You execute a SELECT statement that returns an object of the class SapDB_ResultSet (result set) and query information about the structure of the result set.

Procedure

...

       1.      Create a Python script sample.py with the following contents:

#

# Import Python modules

# -------------------------------------------

import sys

import sdb.sql

#

# Connect to the database instance

# -------------------------------------------

database_user = sys.argv [1]

database_user_password = sys.argv [2]

database_name = sys.argv [3]

session = sdb.sql.connect (database_user, database_user_password, database_name)

select = """

   SELECT title, name, cno FROM hotel.customer

   WHERE cno BETWEEN 3000 AND 8000"""

#

# Execute a SELECT statement

# Result: object of the class SAPDB_ResultSet

# ----------------------------------------------

cursor = session.sql (select)

#

# The getDescription method returns

# a list of the column descriptions.

# ----------------------------------------------

descriptions = cursor.getDescription ()

for description in descriptions:

    print '======================='

    # Column name

    print 'Name:', description [0]

    # Type of the column as a string

    print 'Type:', description [1]

    # Type of the column as an integer

    print 'Type:', description [2]

    # Column length

    print 'Len:', description [3]

    # Number of decimal places

    print 'Frac:', description [4]

    # Result set columns are always required columns.

    print 'Mandatory:', description [5]

    # Result set columns are always output.

    print 'in/out:', description [6]

#

# Close the session with the database instance

# --------------------------------------------

session.release ()

       2.      Call the Python script:

python sample.py MONA RED DEMODB

Result

=======================

Name: TITLE

Type: Char

Type: 1

Len: 7

Frac: 0

Mandatory: 1

in/out: OUT

=======================

Name: NAME

Type: Char

Type: 1

Len: 20

Frac: 0

Mandatory: None

in/out: OUT

=======================

Name: CNO

Type: Fixed

Type: 3

Len: 4

Frac: 0

Mandatory: None

in/out: OUT