 
 |
You Are Here: SAP DB > 7.3 > Interfaces > sapdb > Example
Module sapdb (Example)
#
# egSQL.py call as
# python egSQL.py <user> <pwd> <db> [<host>]
#
import sys
import sapdb
user = sys.argv [1]
pwd = sys.argv [2]
dbname = sys.argv [3]
if len (sys.argv) > 4:
host = sys.argv [4]
else:
host = ''
# connect to db
session = sapdb.connect (user, pwd, dbname, host)
# execute simple command
session.sql ('vtrace')
# get one value
stmt = 'SELECT FIRST msgtext INTO ? FROM messages'
print "\nGetting single value '%s'" % stmt
result = session.sql (stmt)
# tuple with one element
print "\tresult: '%s', %s" % (result , type (result))
# get multiple values
stmt = 'SELECT FIRST msgno, msgtext INTO ?, ? '
+ 'FROM messages'
print "\nGetting multiple values '%s'" % stmt
result = session.sql (stmt)
# tuple with two elements
print "\tresult: '%s', %s" % (result , type (result))
# create a result set
stmt = 'SELECT msgno, msgtext FROM messages '
+ 'WHERE msgno BETWEEN 200 AND 700'
print "\nCreating a result set '%s'" % stmt
result = session.sql (stmt)
# should be a SapDB_ResultSet
print "\tresult: '%s', %s" % (result , type (result))
print '\nfetching values ...'
row = result.next ()
while row:
print "\t", row
row = result.next ()
print '\nfetching again using "for ... in ..."'
for msgno, msgtext in result:
print "[%6d] %s" % (msgno, msgtext)
# parse/execute
stmt = 'SELECT FIRST msgno, msgtext INTO ?, ? '
+ 'FROM messages WHERE msgno BETWEEN ? AND ?'
print "\nParsing '%s'" % stmt
prepared = session.prepare (stmt)
values = [200, 700]
print '\nExecuting with values:', values
result = prepared.execute (values)
print "\t", 'result:', result
print '\nClosing prepared'
prepared = None
# if this was the last reference,
# then the object is destroyed
# simplified executing with arguments
print "\nparse/execute in one call '%s'" % stmt
result = session.sqlX (stmt, values)
print "\t", 'result:', result
session.release ()
print '\nSession released'
|
|
|