python get table’s csv format from oracle

以下的脚本从oracle数据库中查询表的数据生成在本地形成excel格式。主要使用python的三个模块,cx_oracle,os和csv模块。

#!/usr/bin/env python
# -*- coding: utf8 -*-
#Author Leo
#Date 2016.09.01

import cx_Oracle
import csv
import os

SQL_TEXT = "select * From hr.employees"

connection = cx_Oracle.connect('blue','blue','scan_name:1521/service_name')

with open("C:\\t1.csv","wb") as datacsv:
    output=csv.writer(datacsv, dialect='excel')

    cursor = connection.cursor()
    cursor.execute(SQL_TEXT)
    for i in cursor:
        output.writerow(i)
    
    cursor.close()
    connection.close()
    datacsv.close()
此条目发表在Python分类目录。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s