[Python] Python通过cx_Oracle模块将oracle数据表中的数据导出到csv文件 →→→→→进入此内容的聊天室

来自 , 2020-04-18, 写在 Python, 查看 101 次.
URL http://www.code666.cn/view/0678ca2e
  1. # Export Oracle database tables to CSV files
  2. # FB36 - 201007117
  3.  
  4. import sys
  5. import csv
  6. import cx_Oracle
  7.  
  8. connection = raw_input("Enter Oracle DB connection (uid/pwd@database) : ")
  9. orcl = cx_Oracle.connect(connection)
  10. curs = orcl.cursor()
  11.  
  12. printHeader = True # include column headers in each table output
  13.  
  14. sql = "select * from tab" # get a list of all tables
  15. curs.execute(sql)
  16.  
  17. for row_data in curs:
  18.     if not row_data[0].startswith('BIN$'): # skip recycle bin tables
  19.         tableName = row_data[0]
  20.  
  21.         # output each table content to a separate CSV file
  22.         csv_file_dest = tableName + ".csv"
  23.         outputFile = open(csv_file_dest,'w') # 'wb'
  24.         output = csv.writer(outputFile, dialect='excel')
  25.         sql = "select * from " + tableName
  26.         curs2 = orcl.cursor()
  27.         curs2.execute(sql)
  28.  
  29.         if printHeader: # add column headers if requested
  30.             cols = []
  31.             for col in curs2.description:
  32.                 cols.append(col[0])
  33.             output.writerow(cols)
  34.  
  35.         for row_data in curs2: # add table rows
  36.             output.writerow(row_data)
  37.  
  38.         outputFile.close()
  39.  
  40. #//python/6681

回复 "Python通过cx_Oracle模块将oracle数据表中的数据导出到csv文件"

这儿你可以回复上面这条便签

captcha