# -*- coding: utf-8 -*-
import cx_Oracle
from pprint import pprint
import csv
import time
import re
import binascii
conn = cx_Oracle.connect('system/oracle@192.168.137.2/serv')
cursor = conn.cursor()
table_name="t300"
owner="system"
f = open(table_name+".csv", "w")
writer = csv.writer(f, lineterminator="
", quoting=csv.QUOTE_NONNUMERIC)
xsql="select COLUMN_NAME from dba_tab_columns where table_name=upper("+"'"+table_name+"'"+")and owner=upper("+"'"+owner+"'"+")"+" "+"order by column_id asc"
print xsql
r = cursor.execute(xsql)
columns=[];
for (col,) in r:
print col
columns.append(col)
ysql="select COLUMN_NAME,DATA_TYPE from dba_tab_columns where table_name=upper("+"'"+table_name+"'"+")and owner=upper("+"'"+owner+"'"+")"+" "+"order by column_id asc"
d={}
r = cursor.execute(ysql)
m=0
arr1=[]
for j in r:
#print j
if (j[1]=='RAW'):
print j[1]
arr1.append(m)
else:
pass
m = m + 1
print arr1
print '------------------'
exportOracleSql="SELECT ";
for i in range(0, len(columns)):
if i != len(columns) - 1:
exportOracleSql=exportOracleSql+columns[i]+','
else:
exportOracleSql = exportOracleSql + columns[i]
exportOracleSql=exportOracleSql+" "+"from"+" "+owner+"."+table_name
print exportOracleSql
cursor.execute("alter session set nls_date_format='yyyy-mm-dd'")
s1 = cursor.execute(exportOracleSql)
for row in s1:
#print type(row)
s2=[]
for i in range(0,len(row)):
if (i in arr1):
#print (binascii.b2a_hex(row[i]))
s2.append(binascii.b2a_hex(row[i]))
#writer.writerow( binascii.b2a_hex(row[i]))
else:
#print row[i]
s2.append(row[i])
#writer.writerow(row[i])
print s2
writer.writerow(s2)
f.close()
cursor.close()
close__ = conn.close()