Entering content frame

 Python: Example 4 

Example of calling the Loader with Python Script

Start a database session
Log on to the database instance
Create a table and query the error code
Load data to the table using Loader commands and query the error code
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))

# Query whether the table exists by querying the error code
# The sql method is used to do this
# -------------------------------------

rc = session.sql("EXISTS TABLE CUSTOMER")

If rc!=0

   # Then branch of the If statement must be indented in Python
   # Create the table CUSTOMER
   # ----------------------------------------------------------

   rc = session.sql( """CREATE TABLE customer (
                       cno             FIXED(4),
                       name            CHAR(10) ASCII,
                       zip             CHAR(5)  ASCII,
                       city            CHAR(12) ASCII,
                       PRIMARY KEY (cno) """)   

print rc

If rc==0

   # Then branch of the If statement must be indented in Python
   # Load the table CUSTOMER
   # ----------------------------------------------------------

   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 loadrc 

session.cmd ("COMMIT")

# End the database session
# ------------------------

del session

 

Leaving content frame