用两种不同的方法导出ORACLE 查询数据为CSV 文件 (python 代码 与 使用 utl_file 包)
生活随笔
收集整理的這篇文章主要介紹了
用两种不同的方法导出ORACLE 查询数据为CSV 文件 (python 代码 与 使用 utl_file 包)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
因要對(duì)客戶發(fā)送一些推廣郵件,需要把email導(dǎo)出到csv,再使用專門郵件發(fā)送軟件進(jìn)行發(fā)送。
在使用ORACLE 的 utl_file 包,把數(shù)據(jù)寫到文件中。
看到是逐條數(shù)據(jù)寫入文件。速度比較慢。所以想到python來(lái)進(jìn)行處理。寫完后,速度有所改觀。
在寫成BOLG時(shí),SQL 代碼有所修改。
以下是兩種方法。
一、安裝:cx_Oracle-5.1.3
---------------------------------------------------------------
1 下載地址:
?? ?https://pypi.python.org/pypi/cx_Oracle/5.1.3
2 解壓:
[oracle@dg1 backup]$ tar -xvf cx_Oracle-5.1.3.tar.gz
3.編譯并安裝。
[oracle@dg1 cx_Oracle-5.1.3]$ python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
creating build
creating build/temp.linux-x86_64-2.4-11g
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/opt/app/oracle/product/11.2.0/rdbms/demo -I/opt/app/oracle/product/11.2.0/rdbms/public -I/usr/include/python2.4 -c cx_Oracle.c -o build/temp.linux-x86_64-2.4-11g/cx_Oracle.o -DBUILD_VERSION=5.1.3
creating build/lib.linux-x86_64-2.4-11g
gcc -pthread -shared build/temp.linux-x86_64-2.4-11g/cx_Oracle.o -L/opt/app/oracle/product/11.2.0/lib -lclntsh -o build/lib.linux-x86_64-2.4-11g/cx_Oracle.so
[oracle@dg1 cx_Oracle-5.1.3]$ python setup.py install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-2.4-11g/cx_Oracle.so -> /usr/lib64/python2.4/site-packages
error: /usr/lib64/python2.4/site-packages/cx_Oracle.so: Permission denied
4.沒有權(quán)限,換到ROOT用戶再試。
[root@dg1 cx_Oracle-5.1.3]# python setup.py build
Traceback (most recent call last):
? File "setup.py", line 135, in ?
??? raise DistutilsSetupError("cannot locate an Oracle software " \
distutils.errors.DistutilsSetupError: cannot locate an Oracle software installation
5.添加ORACLE 目錄 設(shè)置后,再試
[root@dg1 cx_Oracle-5.1.3]# ORACLE_HOME=/opt/app/oracle/product/11.2.0; export ORACLE_HOME
[root@dg1 cx_Oracle-5.1.3]# echo $ORACLE_HOME
/opt/app/oracle/product/11.2.0
[root@dg1 cx_Oracle-5.1.3]# python setup.py build
running build
running build_ext
[root@dg1 cx_Oracle-5.1.3]# python setup.py install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-2.4-11g/cx_Oracle.so -> /usr/lib64/python2.4/site-packages
二、使用python 連接ORACLE 并導(dǎo)出查詢數(shù)據(jù)
?? ?--------exp.py -----------------------------------------
?? ?import sys
?? ?import csv
?? ?import cx_Oracle
?? ?db=cx_Oracle.connect('tang','tang','192.168.2.100:1521/orcl')
?? ?#print db.version
?? ?printHeader= True
?? ?csv_file ="test.csv"
?? ?outputFile =open(csv_file,'w')
?? ?output = csv.writer(outputFile,dialect='excel')
?? ?sql='select * from (select email from user? c? where length(email)>3 ) '
?? ?curs = db.cursor()
?? ?curs.execute(sql)
?? ?if printHeader:
?? ? cols=[]
?? ? for col in curs.description:
?? ?? cols.append(col[0])
?? ? output.writerow(cols)
?? ?for row_data in curs:
?? ? output.writerow(row_data)
?? ?outputFile.close()
?? ?db.close()
三、原使用 ORACLE dbms_output 包導(dǎo)出數(shù)據(jù)的方法:
------------exp.tst------------------
declare
? P_FILENAME VARCHAR2(20);
? v_value?? varchar2(800);?????????????????????? --記錄的數(shù)據(jù)
? v_sql??? varchar2(4000); ?
? v_fname varchar2(50);
?? type t_cursor is ref cursor ;
?? mycursor t_cursor;
?? outf utl_file.file_type;
?begin
?
? DBMS_OUTPUT.ENABLE(1000000);? --加大緩存
?v_fname:='test.csv';
? v_sql:=
? 'select email from user? c '||' where length(c.email)>3 )';
?dbms_output.put_line(v_sql);
?outf:=utl_file.fopen('DIR_ORACLE_USER',v_fname,'w');
?open mycursor for v_sql;
?loop
?? fetch mycursor into v_value;
?? exit when mycursor%notfound;
?? utl_file.put_line(outf,v_value);
?end loop;
?utl_file.fclose(outf);?????????????? ?
?
end;
總結(jié)
以上是生活随笔為你收集整理的用两种不同的方法导出ORACLE 查询数据为CSV 文件 (python 代码 与 使用 utl_file 包)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我的第一个python 代码
- 下一篇: 【知识小课堂】 mongodb 之 o