Python安装cx_Oracle

Python安装cx_Oracle

𝓓𝓸𝓷 Lv6
一、安装pip
1.pip介绍

pip类似RedHat里面的yum,安装Python包非常方便

2.pip下载
1
# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
3.安装pip
1
2
3
4
5
# yum install libffi-devel 

# tar -xzvf pip-1.5.4.tar.gz -C /opt/
# cd /opt/pip-1.5.4
# python setup.py install
4.pip查看已安装的包
1
# pip show --files SomePackage
5.pip检查哪些包需要更新
1
# pip list --outdated
6.pip升级包
1
# pip install --upgrade SomePackage
7.pip卸载包
1
# pip uninstall SomePackage
8.pip安装案例
1
2
3
4
(1)在线安装
# pip install cx_Oracle
(2)下载第三方whl包安装
# pip install /opt/soft/cx_Oracle-6.4.1-cp27-cp27m-manylinux1_x86_64.whl
二、下载cx_Oracle
1
https://pypi.org/project/cx_Oracle/#files
三、安装cx_Oracle
1
# pip install /opt/soft/cx_Oracle-6.4.1-cp27-cp27mu-manylinux1_x86_64.whl 
四、cx_Oracle模块使用
1
2
3
4
5
[root@db ~]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
五、Python连接
1.连接步骤
  • 引用模块cx_Oracle

  • 连接数据库

  • 获取cursor

  • 使用cursor进行各种操作

  • 关闭cursor

  • 关闭连接

2.连接Oracle
1
2
3
4
5
6
7
import cx_Oracle                                          #引用模块cx_Oracle
conn=cx_Oracle.connect('scott/scott@localhost/db') #连接数据库
c=conn.cursor() #获取cursor
x=c.execute('select sysdate from dual') #使用cursor进行各种操作
x.fetchone()
c.close() #关闭cursor
conn.close()
六、连接错误解决方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[oracle@db ~]$ python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> conn=cx_Oracle.connect('scott/scott@localhost/db')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help


解决办法:
[root@db ~]# locate libclntsh.so
/opt/app/oracle/product/11.2.0/db_1/inventory/Scripts/ext/lib/libclntsh.so.11.1
/opt/app/oracle/product/11.2.0/db_1/inventory/backup/2018-05-31_01-40-59PM/Scripts/ext/lib/libclntsh.so.11.1
/opt/app/oracle/product/11.2.0/db_1/lib/libclntsh.so
/opt/app/oracle/product/11.2.0/db_1/lib/libclntsh.so.10.1
/opt/app/oracle/product/11.2.0/db_1/lib/libclntsh.so.11.1

[oracle@db ~]$ vim .bash_profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/app/oracle/product/11.2.0/db_1/lib/
  • Title: Python安装cx_Oracle
  • Author: 𝓓𝓸𝓷
  • Created at : 2024-06-16 19:45:36
  • Updated at : 2024-07-20 05:15:41
  • Link: https://www.zhangdong.me/python-cx_oracle.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论