You can use the for loop to run through a result set in a loop.
1. Creating the Python file sample_3.py:
#
# To reference Python libraries:
# -------------------------------------------
import sys
import sapdb.sql
#
# Starting the session with the database instance
# -------------------------------------------
database_user = sys.argv [1]
database_user_password = sys.argv [2]
database_name = sys.argv [3]
session = sapdb.sql.connect (database_user, database_user_password, database_name)
select = """
SELECT msgno, language, msgtext FROM messages
WHERE msgno BETWEEN -1000 AND -1"""
#
# If the SQL method executes a SELECT statement,
# the result is an object of the SAPDB_ResultSet class.
-----------------------------------------------
cursor = session.sql (select)
#
# You can use a SapDB_ResultSet object in a for loop
# in the same way as any other iterator.
-----------------------------------------------
for row in cursor:
errorcode, language, message = row
print "%6d %s" % (errorcode, message)
#
# You can use a for statement to
# execute an SQL statement and to group the results rows
# in columns.
-------------------------------------------------
for errorcode, language, message in session.sql (select):
print "%6d %s" % (errorcode, message)
#
# Ending the session with the database instance
# --------------------------------------------
session.release ()
2. To call the Python file sample_3.py from the command line and transfer database user MONA’s data to the Python program with the password RED and the database instance named DEMODB, specify the following:
python sample_3.py MONA RED DEMODB