Example of
calling the Loader with Python Script
If a command is changed at runtime, for example, because user inputs are part of the command, syntax errors can easily occur.
The SQL method only provides SQL error codes. You need exceptions to intercept syntax errors or other Loader errors in the
Create a database session
Log on to the database instance
Use Loader command(s) to load data into table and intercept exceptions
Log off
# Reference to the Python Libraries
# ---------------------------------
import sys
import loader
# Parse the call arguments
# ------------------------
userid = sys.argv [1]
password = sys.argv [2]
database_name = sys.argv [3]
data_path = sys.argv[4]
server_node = ''
# Create a database session for the Loader
# -------------------------------------------
session = loader.Loader (server_node, database_name)
# Log on to the database instance
# --------------------------------
session.cmd ('use user %s %s;' % (userid, password))
# Example of Exception Handling
# -----------------------------
try:
loadrc = session.sql ("""DATALOAD TABLE customer
cno 1-4
name 6-12
zip 14-18
city 20-31
INFILE %s\customer.dat""" %data_path )
print "'%s'" % loadrc
except loader.LoaderServError, err:
print 'command failed:', err
# End the database sessio
ndel session