Python: Example No. 4 

Set up a user session
Log on to the database instance
Create a table and query error code
Use
REPMServer command(s) to load data into table and query error code
Log off

 

# Reference to Python Libraries

# ----------------------------

import sys

import repman

 

# Parse the call arguments

# --------------------------

user = sys.argv [1]

pwd = sys.argv [2]

dbname = sys.argv [3]

data_path = sys.argv[4]

host = ''

 

# Connect to Replication Manager

# A new instance of the repman object is created

# Host determines the location of the REPMServer

# ------------------------------------------------------

session = repman.RepMan (host, dbname)

 

# Connect to database

# The cmd method is used for this purpose

# -------------------------------------

session.cmd ('use user %s %s;' % (user, pwd))

 

# Query error code to determine whether table exists

# The sql method is used for this purpose

# ----------------------------------------------------------

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),

surname CHAR(10) ASCII,

zip CHAR(5) ASCII,

place CHAR(12) ASCII,

PRIMARY KEY (cno) """)

 

print rc

 

If rc==0

# Then branch of the If statement must be indented in Python

# Load table CUSTOMER

# ----------------------------------------------------------

loadrc = session.sql ("""DATALOAD TABLE customer

cno 1-4

surname 6-12

zip 14-18

place 20-31

INFILE %s\customer.dat""" %data_path )

print loadrc

 

 

session.cmd ("COMMIT")

 

# Log off by releasing the instance

# -------------------------------------

del session