cx_Oracle for python

2014-02-13

cx_Oracle是一个用来连接并操作Oracle数据库的Python扩展模块,支持包括Oracle9.2, 10g以及11g等版本。

cx_Oracle 7 need Python 2.7 or 3.5 and higher,Older versions of cx_Oracle may work with older versions of Python.

#!/usr/bin/python

import cx_Oracle

print cx_Oracle.version;

print dir(pyev)

help(cy_Oracle)

 

1、下载网址:
http://sourceforge.net/projects/cx-oracle

#!/bin/sh

yum -y install git python-devel libaio libaio-devel

git clone https://github.com/oracle/python-cx_Oracle.git cx_Oracle
cd cx_Oracle
git submodule init
git submodule update
python setup.py install

2、unzip instantclient-basic-macos.x64-12.2.0.1.0.zip

Add links to $HOME/lib or /usr/local/lib to enable applications to find the library

vi /etc/ld.conf.d/ora.conf

ldconfig

3、mkdir -p /opt/oracle/instantclient_12_2/network/admin

tnsnames.ora

Then set the environment variable

export TNS_ADMIN=$ORACLE_HOME/network/admin

4、实例:

more alter_viewer_page_id.py
#!/usr/bin/env python

import cx_Oracle
import date

ym=time.strftime('%Y%m')

sql='alter table VIEWER_'+ ym + ' modify PAGE_ID NUMBER(12)'

conn = cx_Oracle.connect('viewer/123456@192.168.1.32:1521/mydb')
cursor = conn.cursor()
cursor.execute(sql)
cursor.close()

 

5、

#!/usr/bin/python
import cx_Oracle

conn = cx_Oracle.connect('tuser/123455@172.16.1.206:1521/dbpri')

cursor = conn.cursor()

cursor.execute('Select tablespace_name,file_name from dba_data_files')

for ts_name,fname in cursor:
print(ts_name,fname)
cursor.close()

6、

#!/usr/bin/python
import cx_Oracle
#conn = cx_Oracle.connect('tu/123456@1.1.1.2:1521/mydb')
#conn = cx_Oracle.connect('tu/123456@db')
conn = cx_Oracle.connect('tuser/9911@ip:1521/idb')

cursor = conn.cursor()

#result=cursor.execute('Select count(*) from test')
#result=
cursor.execute('Select tablespace_name,file_name from dba_data_files')

#data=cursor.fetchone()
#print (data)
all_data=cursor.fetchall()
print(all_data)
#many_data=cursor.fetchmany(8)
#print (many_data)
cursor.close()

分类:数据库编程 | 标签: |

相关日志

评论被关闭!