cx_Oracle

cx_Oracle

安装

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的错误

[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

[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

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

显示出oracle的版本号

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

使用

可以参看
http://www.oracle.com/technetwork/articles/dsl/python-091105.html

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

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()

拿来做简单的查询是够了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值