python指令碼直接匯出oracle table到csv(./oracletest.py 表名)
阿新 • • 發佈:2018-11-07
#!/usr/bin/env python
##saveas oracletest.py
##chmod 755 oracletest.py
##./oracletest.py ab01_
import sys
import csv
import cx_Oracle
import codecs
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.ZHS16GBK'
conn = cx_Oracle.connect("vivigsrp", "vivigsrp", "10.1.1.210/orcl")
curs = conn .cursor()
tabname=sys.argv[1]
csv_file_dest = "/home/hadoop/oracledata/"+tabname+ ".csv"
outputFile = open(csv_file_dest,'w') # 'wb'
output = csv.writer(outputFile, dialect='excel')
sql = "select * from "+tabname # get a list of all tables
curs.execute(sql)
cols = []
for col in curs.description:
cols.append(col[0])
output.writerow(cols)
for row_data in curs: # add table rows
output.writerow(row_data)
outputFile.close()