[关闭]
@tony-yin 2017-12-07T14:17:10.000000Z 字数 3487 阅读 718

Python连接Oracle

Python Oracle


当前环境:Linux Centos 7

1. 下载安装包cx_Oracle

由于我本地Python版本是2.7,所以选择是2.7版本

  1. wget https://pypi.python.org/packages/e1/18/00987c6a9af9568ee87d1fcba877407684a3f1b87515e5eb82d5d5acb9ff/cx_Oracle-6.0rc1-py27-1.x86_64.rpm#md5=ff8f5cce07bd5ec8ed3ce5c79d451474

参考:

2. 安装cx_Oracle

  1. rpm -ivh cx_Oracle-6.0rc1-py27-1.x86_64.rpm
  1. [root ~]# python
  2. Python 2.7.5 (default, Nov 6 2016, 00:28:07)
  3. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import sys
  6. >>> print sys.path
  7. ['', '/usr/lib/python2.7/site-packages/AliyunUtil-0.0.1-py2.7.egg', '/usr/lib/python2.7/site-packages/cloud_init-0.7.6-py2.7.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']
  1. [root ~]# rpm -qa | grep cx_Oracle | xargs rpm -ql
  2. /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info
  3. /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/PKG-INFO
  4. /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/SOURCES.txt
  5. /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/dependency_links.txt
  6. /usr/lib64/python2.7/site-packages/cx_Oracle-6.0rc1-py2.7.egg-info/top_level.txt
  7. /usr/lib64/python2.7/site-packages/cx_Oracle.so

3. 下载 Oracle Library

由于Oracle官网下载东西存在Auth认证,所以简单点就点击选中的rpm包保存在windows上,然后上传到linux上即可
注意:

  • 下载的版本要和连接的Oracle基本一致,我需要连接的Oracle版本是11.2,所以我下载的是oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
  • 操作系统要和之前下载的cx_Oracle要一致,这很重要!!!我在这个上面栽了跟头,花了比较多的时间。。。所以我需要下载64位的
  • Oracle官网下载东西你得先勾选Accept License Agreement,否则你下载不了
  • 其次你得有Oracle的账号

4. 安装 Oracle Library

  1. rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm

5. 配置环境变量

  1. cd /usr/lib/oracle/11.2/
  2. mv client64 client
  1. vim /etc/profile
  1. // 查看Oracle安装路径
  2. [root@iZbp180vortqapb7yf8217Z ~]# rpm -qa | grep oracle | xargs rpm -ql
  3. /usr/lib/oracle/11.2/client64/bin/adrci
  4. /usr/lib/oracle/11.2/client64/bin/genezi
  5. /usr/lib/oracle/11.2/client64/lib/libclntsh.so.11.1
  6. /usr/lib/oracle/11.2/client64/lib/libnnz11.so
  7. /usr/lib/oracle/11.2/client64/lib/libocci.so.11.1
  8. /usr/lib/oracle/11.2/client64/lib/libociei.so
  9. /usr/lib/oracle/11.2/client64/lib/libocijdbc11.so
  10. /usr/lib/oracle/11.2/client64/lib/ojdbc5.jar
  11. /usr/lib/oracle/11.2/client64/lib/ojdbc6.jar
  12. /usr/lib/oracle/11.2/client64/lib/xstreams.jar
  13. // 追加ORACLE_HOME环境变量路径
  14. export ORACLE_HOME=/usr/lib/oracle/11.2/client64
  15. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib
  1. source ~/.bashrc

6. 检查是否可以引入cx_Oracle

import没有报错,说明之前的辛苦没有白费

  1. [root client64]# python
  2. Python 2.7.5 (default, Nov 6 2016, 00:28:07)
  3. [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import cx_Oracle
  6. >>>

7. 连接 Oracle

一个插入10条数据的Python脚本demo:

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import sys
  4. import cx_Oracle
  5. import time
  6. rows = int(sys.argv[1])
  7. con = cx_Oracle.connect('test/test@sampledb')
  8. cur = con.cursor()
  9. cur.execute("SELECT MAX(ID) FROM PTTEST")
  10. max_id = cur.fetchone()[0] or 0
  11. start_id = max_id + 1
  12. for i in xrange(rows):
  13. cur.execute("INSERT INTO PTTEST (ID, NAME, AGE, GENDER, SALARY)VALUES(%d, 'pt', 15, 'male', 50000)" % int(i + start_id))
  14. if i % 100 == 0:
  15. time.sleep(3)
  16. print 'Insert rows [%d]:%d' % (int(time.time()), i)
  17. con.commit()
  18. cur.close()
  19. con.close()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注