Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python使用cx_Oracle模块将oracle中数据导出到csv文件的方法。分享给大家供大家参考。具体实现方法如下:

# Export Oracle database tables to CSV files
# FB36 - 201007117
import sys
import csv
import cx_Oracle
connection = raw_input("Enter Oracle DB connection (uid/pwd@database) : ")
orcl = cx_Oracle.connect(connection)
curs = orcl.cursor()
printHeader = True # include column headers in each table output
sql = "select * from tab" # get a list of all tables
curs.execute(sql)
for row_data in curs:
  if not row_data[0].startswith('BIN$'): # skip recycle bin tables
    tableName = row_data[0]
    # output each table content to a separate CSV file
    csv_file_dest = tableName + ".csv"
    outputFile = open(csv_file_dest,'w') # 'wb'
    output = csv.writer(outputFile, dialect='excel')
    sql = "select * from " + tableName
    curs2 = orcl.cursor()
    curs2.execute(sql)
    if printHeader: # add column headers if requested
      cols = []
      for col in curs2.description:
        cols.append(col[0])
      output.writerow(cols)
    for row_data in curs2: # add table rows
      output.writerow(row_data)
    outputFile.close()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python中的split()函数和os.path.split()函数使用详解

Python中有split()和os.path.split()两个函数: split():拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表。 os.path.spli...

pycharm 将django中多个app放到同个文件夹apps的处理方法

pycharm 将django中多个app放到同个文件夹apps的处理方法

在django中需要创建多个app,这个就需要创建一个apps文件,把所有的app放到同个文件夹,这个比较清楚,看起来也比较规范 首先在项目文件右击—–new–python packag...

python3使用tkinter实现ui界面简单实例

python3使用tkinter实现ui界面简单实例

复制代码 代码如下:import timeimport tkinter as tkclass Window:    def __init__(self,...

利用Python中unittest实现简单的单元测试实例详解

前言 单元测试的重要性就不多说了,可恶的是Python中有太多的单元测试框架和工具,什么unittest, testtools, subunit, coverage, testrepos...

pytorch 自定义卷积核进行卷积操作方式

pytorch 自定义卷积核进行卷积操作方式

一 卷积操作:在pytorch搭建起网络时,大家通常都使用已有的框架进行训练,在网络中使用最多就是卷积操作,最熟悉不过的就是 torch.nn.Conv2d(in_channels,...