python安装oracle驱动_Python安装Oracle数据库驱动
1.環(huán)境設(shè)置
[root@oracle ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@oracle ~]# python -V
Python 2.6.6
版本:Oracle 12c
2.前提:安裝cx_Oracle模塊依賴包
由于使用Python連接Oracle,所以需要下載oracle客戶端包
oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm
1
[root@oracle?~]#?rpm?-?ivh?oracle-instantclient12.1-*
3.設(shè)置環(huán)境變量
[root@oracle ~]# cat ~/.bash_profile
1
2
export?/usr/lib/oracle/12.1/client64/
export?LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib
4.安裝cx_Oracle模塊
官網(wǎng):https://pypi.python.org/pypi/cx_Oracle
cx_Oracle-6.2.1.tar.gz
1
2
[root@oracle?~]#?tar?-zxvf?cx_Oracle-6.2.1.tar.gz
[root@oracle?cx_Oracle-6.2.1]#?python?setup.py?install
可能會(huì)遇到報(bào)錯(cuò)
error: command 'gcc' failed with exit status 1
解決方法:
yum install python-devel
yum install libevent-devel
5.查看是否可以導(dǎo)入cx_Oracle模塊
[root@oracle ~]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>? #無(wú)報(bào)錯(cuò)說(shuō)明成功
6.編寫Python連接oracle數(shù)據(jù)庫(kù)腳本(oracle用戶,因?yàn)槲疫@里都是用oracle用戶權(quán)限安裝的)
[oracle@oracle ~]$ cat py_oracle.py
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env?python
#-*-?coding:?UTF-8?-*-
import?cx_Oracle#導(dǎo)入模塊
db=cx_Oracle.connect('system/oracle@localhost:1521/ORCL')#連接user/passwd@host:端口/instance
cursor?=?db.cursor()#創(chuàng)建游標(biāo)對(duì)象
cursor.execute('select?sysdate?from?dual')#執(zhí)行命令
data?=?cursor.fetchone()#返回值
print('Database?time:%s'?%?data)??打印輸出
cursor.close()#關(guān)閉游標(biāo)對(duì)象
db.close()#關(guān)閉數(shù)據(jù)庫(kù)
7.執(zhí)行腳本
[oracle@oracle ~]$ python py_oracle.py
總結(jié)
以上是生活随笔為你收集整理的python安装oracle驱动_Python安装Oracle数据库驱动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C/C++预处理指令
- 下一篇: 一个计算机台式机的组装方案,既能带又便宜