Procedure documentationExample: Executing an SQL Statement and Querying Information About the Result Set Locate this document in the navigation structure

 

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

Procedure

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

    Syntax Syntax

    1. #
      # Import Python modules
      # -------------------------------------------
      import sys
      import sdb.sql
      #
      # Connect to the database
      # -------------------------------------------
      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 SAPDB_ResultSet class
      # ----------------------------------------------
      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
      # --------------------------------------------
      session.release ()
      
    End of the code.
  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