Skip to content

Instantly share code, notes, and snippets.

@jamiesun
Created September 21, 2012 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiesun/3760783 to your computer and use it in GitHub Desktop.
Save jamiesun/3760783 to your computer and use it in GitHub Desktop.
oracle export data
#coding:utf-8
import sys
import cx_Oracle
import csv
print '请输入要导出的数据库地址,格式:user/passwd@host:port/sid :'.decode('utf-8')
db = len(sys.argv)>=2 and sys.argv[1] or raw_input('Enter db(user/passwd@host:port/sid):')
connection = cx_Oracle.Connection(db)
cursor = connection.cursor()
sqls = {
"user":" select * from user"
}
#统计信息
statwriter = csv.writer(open("./stat.csv",'wb'),delimiter=',', quotechar='"',quoting=csv.QUOTE_MINIMAL)
for name,sql in sqls.items():
writer = csv.writer(open("./%s.csv"%(name.decode('utf-8')), "wb"),delimiter=',', quotechar='"',quoting=csv.QUOTE_ALL)
cursor.execute(sql.decode('utf-8'))
print "-"*80
print "export sql:%s"%sql.decode('utf-8')
writer.writerow([(hd[0] or '') for hd in cursor.description])
stat = 0
for row in cursor:
writer.writerow([(rw or '') for rw in row])
stat += 1
statwriter.writerow([name,stat])
cursor.close()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment