# This script prints the data in Oracle Sample table scott.emp .
# Run with a parameter 'PageSize' in integer form, the output pauses at the end of every page.
# Run without a parameter or the parameter is not in integer form, the output doesn't pasue.
# Like:
# $ python select_ora.py 30
# $ python select_ora.py
#
import sys
import cx_Oracle
try:
intPageSize = int(sys.argv[1])
except:
intPageSize = -1
#print "Please input an integer."
#quit()
conn = cx_Oracle.connect('scott/tiger@192.168.150.115/c6115')
cursor = conn.cursor ()
cursor.execute ("select * from emp")
print "EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO"
print "========================================================"
while (1):
row = cursor.fetchone()
if row == None: break
print "%d, %s, %s, %s, %s, %s, %s, %s" % (row[0], row[1], row[2], row[3], row[4],row[5],row[6],row[7])
if intPageSize <> -1 and cursor.rowcount % intPageSize == 0 :
strPress = raw_input( "Row: %d" % cursor.rowcount + ". Press Enter to continue, q to quit..." )
if strPress == 'q': break
print "Number of rows returned: %d" % cursor.rowcount
cursor.close ()
conn.close ()
数据插入示范脚本 insert_ora.py
import cx_Oracle
startNum = raw_input("Start Num:")
endNum = raw_input("End Num:")
conn = cx_Oracle.connect('scott/tiger@192.168.150.115/c6115')
cursor = conn.cursor()
i = int(startNum)
while (1):
i = i+1
if i > int(endNum):
break
theNum=str(i)
cursor.execute("insert into emp (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) values("+theNum+",'FORD"+theNum+"','CLERK',7782,'09-JUN-81',2500,0,30)")
conn.commit()
print "Line "+ theNum +" inserted."
cursor.close()
conn.close()