To create a connection, copy the corresponding arguments from the following examples to the command line.
Use the following command to call the sample.py Python script and log on to the HOTELDB database instance with DBM as the user name and password.
python sample.py DBM DBM HOTELDB
# To reference Python libraries:
# ----------------------------
import sys
import sapdb.dbm
# To parse call arguments:
# --------------------------
user_name = sys.argv [1]
password = sys.argv [2]
database_name = sys.argv [3]
# To create a Database Manager session:
# ------------------------------------------------------
session = sapdb.dbm.DBM (’’, database_name, ’’, user_name + ’,’ + password)
# To log off:
# -------------------------------------
session.release ()
The following examples specify a shortened login process:
user_name, password, database_name = sys.argv [1:4]
session = sapdb.dbm.DBM (’’, database_name, user_name + ’,’ + password)
You can use the cmd method to execute Database Manager statements. The result is a character string that can be further processed using Python.
# To reference Python libraries:
# ----------------------------
import sys
import sapdb.dbm
# To create a Database Manager session:
# ------------------------------------------------------
user_name, password, database_name = sys.argv [1:4]
session = sapdb.dbm.DBM (’’, database_name, ’’, user_name + ’,’ + password)
# To execute the command that
# lists all database instances: (The result is a character string.)
output = session.cmd ('db_enum')
dbstate = 'offline'
lastdb = ''
# Individual database instances are separated by line breaks.
for line in output.split ('\n'):
if not line:
continue
# Data fields are separated by tab characters.
name, instroot, release, kind, state = line.split ('\t')
if name != lastdb:
# Several lines exist for each database instance,
# max. one line for each of the following core variants:
# fast, quick, slow and test
if lastdb != '':
print lastdb, '\t', dbstate
lastdb = name
dbstate = 'offline'
# The database is active if one of the core variants
# is displayed as 'running'.
if state == 'running':
dbstate = state
print lastdb, '\t', dbstate
If an error occurs within the cmd method, an exception object of the DBMServError class is generated. This exception can be identified and handled within a try-except statement.
# To reference Python libraries:
# ----------------------------
import sys
import sapdb.dbm
# To create a Database Manager session:
# ------------------------------------------------------
user_name, password, database_name = sys.argv [1:4]
session = sapdb.dbm.DBM (’’, database_name, ’’, user_name + ’,’ + password)
for cmd in ['db_state', 'invalid command']:
try:
result = session.cmd (cmd)
# To output the result:
print cmd + ': OK ', repr (result)
except sapdb.dbm.DBMServError, err:
# To output the error message:
print cmd + ': ERR', err.errorCode, err.message