您的位置:首页 > 数据库 > Oracle

cx_Oracle

2015-06-12 09:50 204 查看

cx_Oracle

安装

[code]pip install cx_Oracle


不过我没用那个成功安装过。我找了rpm 包,

http://nchc.dl.sourceforge.net/project/cx-oracle/5.1.2/cx_Oracle-5.1.2-11g-py26-1.x86_64.rpm

然后直接rpm安装了你懂的。

不过你在python中执行import cx_Oracle时还会报找不到lib的错误

[code][root@iZ23dnwecebZ ~]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory


其实libclntsh.so.11.1这个是有的,用find 可以找到。

/data/oms/agent/core/12.1.0.2.0/instantclient/libclntsh.so.11.1

把这个目录加到/etc/ld.so.conf

[code][root@iZ23dnwecebZ ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
/data/oms/agent/core/12.1.0.2.0/instantclient
[root@iZ23dnwecebZ ~]# ldconfig


之后执行import cx_Oracle就没有问题了。

写个小的test程序去连一下oracle

[code]import cx_Oracle
db = cx_Oracle.connect('system', 'oracle', '10.168.xx.xx:1521/xxx')
print db.version


显示出oracle的版本号

[code][oracle@iZ23dnwecebZ ~]$ python t.py
11.2.0.4.0


使用

可以参看

http://www.oracle.com/technetwork/articles/dsl/python-091105.html

fetchone,fetchall的用法和mysql的python调用差不多。我也只是用作最简单的查询。

基本用法:

[code]import cx_Oracle
con = cx_Oracle.connect('pythonhol/welcome@127.0.0.1/orcl')
cur = con.cursor()
sql='select * from departments order by department_id'

cur.execute(sql)             
res = cur.fetchall()
for r in res
    print r

cur.close()           
con.close()


拿来做简单的查询是够了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: