This example describes the following steps:
...
1. Calling the Loader
2. Starting the session with the database instance
3. Creating a table
4. How to load data into a table using Loader commands; an exception-handling process deals with errors
5. Ending the session with the database instance
# Reference to Python Libraries
# ----------------------------
import sys
import sdb.loader
# Calling the Loader; the most up-to-date version of the
# Loader is started; there is still no connection
# to the database
# -------------------------------------
session = sdb.loader.Loader ()
# Starting the session with the database instance
# To make things easier, logon in this example is
# shortened; the parsing of call arguments has
# been omitted
# -------------------------------------
session.cmd ('use user %s %s serverdb %s' % tuple (sys.argv [1:4]))
# How 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'
# Loading the CUSTOMER table: any errors that occur
# are dealt with
# -------------------------------------
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 sdb.loader.LoaderError, err:
print ("DATALOADing file %s failed: %d, %s\n" % (filename, err.errorCode, err.message))
session.cmd ("COMMIT")
# Ending the session with the database instance
# -------------------------------------
del session
Content of the customer.dat File
"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"