This example describes the following steps:
...
1. How to create a database session
2. How to log on to a database instance
3. How to create a table
4. How to load data into a table using Loader commands; an exception-handling process deals with errors
5. How to log off
# To reference Python libraries:
# ----------------------------
import sys
import sapdb.loader
# To create a Loader session: (the most recent version of
# Loader is started; there is still no connection
# to the
database)
# -------------------------------------
session = sapdb.loader.Loader ()
# To log on to the database instance:
# To make the example easier to follow, the logon process has been
# shortened here; the parsing of call arguments has
# also been omitted.
# -------------------------------------
session.cmd ('use user %s %s serverdb %s' % tuple (sys.argv [1:4]))
# To query whether a table exists:
# The sql method is used here;
# if the table does not yet exist, Loader
# returns an error; and the table is
# created.
# -------------------------------------
sqlrc = session.sql("EXISTS TABLE CUSTOMER")
if (sqlrc == 0):
print 'Table CUSTOMER exists'
elif (sqlrc == -4004):
# To create the CUSTOMER table:
# -------------------------------------
session.sql("""
CREATE TABLE customer
(
cno FIXED(4,0) PRIMARY KEY,
title CHAR(7),
firstname CHAR(10),
name CHAR(10) NOT NULL,
zip CHAR(5),
address CHAR(25) NOT NULL,
CONSTRAINT cno_cons CHECK cno > 0,
CONSTRAINT title_cons CHECK title IN
('Mr','Mrs','Company'),
CONSTRAINT zip_cons CHECK
SUBSTR(ZIP,1,1) BETWEEN '1' AND '9' AND
SUBSTR(ZIP,2,1) BETWEEN '0' AND '9' AND
SUBSTR(ZIP,3,1) BETWEEN '0' AND '9' AND
SUBSTR(ZIP,4,1) BETWEEN '0' AND '9' AND
SUBSTR(ZIP,5,1) BETWEEN '0' AND '9'
)""")
print 'Table CUSTOMER created'
# To load the CUSTOMER table: (An exception-handling
# process deals with any errors that occur.)
# -------------------------------------
filename = 'customer.dat'
try:
session.cmd("""
DATALOAD TABLE customer
cno 1
title 2
firstname 3
name 4
zip 5
address 6
INFILE '%s' """ % (filename))
print 'Table CUSTOMER loaded'
except sapdb.loader.LoaderError, err:
print ("DATALOADing file %s failed: %d, %s\n" % (filename, err.errorCode, err.message))
session.cmd ("COMMIT")
# To end the database session:
# -------------------------------------
del session
"3000","Mrs","Jenny","Porter","10580","1340 N.Ash Street, #3"
"3100","Mr","Peter","Brown","48226","1001 34th Str., APT.3"
"3200","Company","?","Datasoft","90018","486 Maple Str."
"3300","Mrs","Rose","Brian","75243","500 Yellowstone Drive, #2"
"3400","Mrs","Mary","Griffith","20005","3401 Elder Lane"
"3500","Mr","Martin","Randolph","60615","340 MAIN STREET, #7"
"3600","Mrs","Sally","Smith","75243","250 Curtis Street"
"3700","Mr","Mike","Jackson","45211","133 BROADWAY APT. 1"
"3800","Mrs","Rita","Doe","97213","2000 Humboldt Str., #6"
"3900","Mr","George","Howe","75243","111 B Parkway, #23"
"4000","Mr","Frank","Miller","95054","27 5th Str., 76"
"4100","Mrs","Susan","Baker","90018","200 MAIN STREET, #94"
"4200","Mr","Joseph","Peters","92714","700 S. Ash Str., APT.12"
"4300","Company","?","TOOLware","20019","410 Mariposa Str., # 10"
"4400","Mr","Antony","Jenkins","20903","55 A Parkway, #15"