Entering content frame

 Python: Example 5 

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 returns SQL error codes. You need exceptions to intercept syntax errors or other Loader errors in the commands.

Start a database session
Log on to the database instance
Load data to the table using Loader commands and intercept exceptions
Log off

# Reference to the Python Libraries
# ---------------------------------

import sys
import loader

# Parse the call arguments
# --------------------------

user_name = 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;' % (user_name, 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 session
# ------------------------

del session

 

Leaving content frame