#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on May 26
@author:Angelevil
""" #Import
module
import cx_Oracle as co
import sys
import os
import csv
import traceback
# Connect to database
ORCL = co.connect('dbuser/dbpwd@ip_address:port/TNS') #Create
cursor
CURS = ORCL.cursor() #Write
SQL statement
SQL = "select * from table_name" #Execute
SQL statement
curs = CURS. execute(SQL) #View
database data
data = curs.fetchall()
# print(data) #Get
the column name of the table
title = [i[0] for i in CURS.description] #Write
data to CSV file
try:
with open("table_name.csv",'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(title)
writer.writerows(data)
except:
print("文件写入数据错误")
finally:
finally :
CURS.close()
ORCL.close()
# -*- coding: utf-8 -*-
"""
Created on May 26
@author:Angelevil
""" #Import
module
import cx_Oracle as co
import sys
import os
import csv
import traceback
# Connect to database
ORCL = co.connect('dbuser/dbpwd@ip_address:port/TNS') #Create
cursor
CURS = ORCL.cursor() #Write
SQL statement
SQL = "select * from table_name" #Execute
SQL statement
curs = CURS. execute(SQL) #View
database data
data = curs.fetchall()
# print(data) #Get
the column name of the table
title = [i[0] for i in CURS.description] #Write
data to CSV file
try:
with open("table_name.csv",'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(title)
writer.writerows(data)
except:
print("文件写入数据错误")
finally:
finally :
CURS.close()
ORCL.close()