首页 > 代码库 > cx_Oracle

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

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

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

cx_Oracle