$wget http://download.oracle.com/otn/linux/instantclient/10204/basic-10.2.0.4.0-linux-x86_64.zip

3.安装配置

$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cd instantclient_10_2
$cp * /usr/lib #直接放到动态库搜索路径中,不需要额外的环境配置 或
$unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
$cp -rf instantclient_10_2 /opt/
$vi /etc/profile
export ORACLE_HOME=/opt/instantclient_10_2
   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME $source /etc/profile

4.配置tnsnames.ora(可不用配置tns)

注意tnsnames.ora其实并不存在,是要自己创建的(这个也很恶心,我一开始以为还要安装什么东东。。),我没有使用这种方式,有兴趣的可以google一下。

5.下载安装cx_Oracle python模块

$wget http://downloads.sourceforge.net/project/cx-oracle/5.1.2/cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
$rpm -ivh cx_Oracle-5.1.2-10g-py26-1.x86_64.rpm
$ls /usr/lib/python2.6/site-packages/cx_Oracle.so #有这个文件表示安装成功,根据python的位置,也可能在其他地方,自己找一下吧

6.验证及问题解决

$python
>>import cx_Oracle

若报错:import cx_Oracle gave ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory

表示没有找到instant client的动态库,check一下环境变量是否配置,是否生效,版本是否正确。

若报错:ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode

Google的信息:There is nothing wrong with Debian. Python supports two incompatible  modes of operation for Unicode, UCS2 (the default), and UCS4. Debian  uses the default, Redhat uses UCS4. You need to recompile the extension  for UCS-2 mode (i.e. using a Debian installation); this would fix the undefined symbol: PyUnicodeUCS4_Decode

所以重新编译python

$./configure --prefix=/usr/local/python2.6.5 --enable-shared -enable-unicode=ucs4
$make;make install

再次验证,终于正常import了。

使用:

1.基本连接–使用Oracle tns alias

connection =cx_Oracle.connect("tp/tp@ocn_test")
#查看tns alias命令
cmd>tnsping ocn_test
TNS Ping Utility forLinux: Version 9.2.0.8.0-Production on 27-SEP-201110:47:48
Copyright (c) 1997, 2006, Oracle Corporation. Allrights reserved.
Used parameter files:
/opt/……/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL =TCP)(HOST =10.20.36.19)(PORT =1520))) (CONNECT_DATA =(SID =ocntest)))
OK (10msec)

2.用户输入密码连接

pwd =getpass.getpass()
connection =cx_Oracle.connect("tp",pwd,"ocn_test")

3.用户直接在Python命令中输入连接账号信息,格式如python script.py tp/tp@ocn_test

connection =cx_Oracle.connect(sys.argv[1])

4.使用Easy Connect语法,通过Drive连接数据库

connection =cx_Oracle.connect('tp','tp','10.20.36.19:1521/ocntest')
#or
connection =cx_Oracle.connect('tp/tp@10.20.36.19:1521/ocntest')

5.先使用DSN构成TNSNAME

tns_name =cx_Oracle.makedsn('10.20.36.19','1521',' ocntest ')
connection =cx_Oracle.connect('tp','tp',tns_name)

6.登陆as SYSDBA

connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSDBA)
#or as SYSOPER
connection =cx_Oracle.connect('tp/tp@ocn_test', mode=cx_Oracle.SYSOPER)

在Linux服务器执行Oracle操作时报了一个错误:

TNS:listener does not currently know of service requested in connect descriptor

解决方式:

问题分析见http://ora-12514.ora-code.com/,一番折腾,最后使用第5种连接方式,瞬间解决此问题。

import cx_Oracle
conn = cx_Oracle.connect('用户名/密码@tnsname里面配置数据库名')
cursor = conn.cursor()
cursor.execute("select to_char(sysdate,'yyyymmdd') from dual")
rs = cursor.fetchone()
print('result = %s'%rs)
result =
cursor.close()
conn.close()

python cx_Oracle模块的安装和使用的更多相关文章

  1. Python 库/模块的安装、查看

    关于如何查看本地python类库详细信息的方法 关于如何查看本地python类库详细信息的方法 - 小白裸奔 - CSDN博客 python -m pydoc -p 1234 help('module ...

  2. python部分模块的安装

    scrapy http://www.cnblogs.com/txw1958/archive/2012/07/12/scrapy_installation_introduce.html pyHook - ...

  3. Python第三方模块tesserocr安装

    介绍 在爬虫过程中,难免会遇到各种各样的验证码,而大多数验证码还是图形验证码,这时候我们可以直接用 OCR 来识别. tesserocr 是 Python 的一个 OCR 识别库 ,但其实是对 tes ...

  4. python 蓝牙模块pybluz安装

    最近项目运用了蓝牙,所以来学一学蓝牙. 经过查阅,知道python的蓝牙模块是pybluz,然后老管理进行安装 出错,提示“Could not find the Windows Platform SD ...

  5. Python——rrdtool模块的安装

    一.Centos环境yum命令安装 yum install rrdtool rrdtool-devel 二.源码安装 wget https://pypi.python.org/packages/99/ ...

  6. python selenium 模块的安装及使用

    安装 pip install selenium 或者到https://pypi.python.org/pypi/selenium 下载setup安装包,之后进入目录后运行python setup.py ...

  7. Python常用模块的安装方法

    http://blog.163.com/yang_jianli/blog/static/161990006201162152724339/

  8. Python 安装cx_Oracle模块

    1.Python安装cx_Oracle模块需要安装Oracle,并在环境变量中添加Oracle的路径. 2.没有安装Oracle的需要下载一个oci.dll的文件,并把文件的路径添加到path中. 如 ...

  9. Python 安装cx_Oracle模块折腾笔记

    kali linux/ubuntu下安装: 不得不说安装这个模块很蛋疼,决定做个记录. sudo apt install build-essential unzip python-dev libaio ...

随机推荐

  1. web.xml文件模板

     Servlet 2.3 deployment descriptor 注:web.xml中提示错误The content of element type "web-app" mus ...

  2. Unity 2d 的 SpriteMask为游戏表现带来多种可能性

    孙广东  2017.7.22 http://blog.csdn.NET/u010019717           SpriteMask 是Unity 2017.1 开始添加2d功能!,    Spri ...

  3. swift metal ios8 关键字.

    swift metal ios8  关键字. 4000API. 无所谓谁打败谁吧. 行业内用户用的多 资源多 问题容易解决. 今年明显unity 火热程度非常. 然,万变不离其中. 对于游戏产品来说, ...

  4. LNMP架构基础搭建

    LNMP架构+wordpress博客 环境: centos6.7 2.6.32-573.el6.x86_64 nginx-1.6.3 mysql-5.5.49 php-5.3.27 wordpress ...

  5. Objective C - 4 - 下载图片并且加载到View

    #import "LoadInternetImageViewController.h" @interface LoadInternetImageViewController () ...

  6. 2017年 ACM Journal Latex templates 新模板生成 acmart.cls 文件

    假定你的文稿在:/user/acmart-master那么cd /user/acmart-masterlatex acmart.ins最后可得到acmart.cls.

  7. MySQL 5.6.30 升级到5.7.10

    MySQL 5.6.30 升级到5.7.10 注意,这种方式的前提是数据文件没有和软件目录在一起,如果在一起,需要停止数据库后先移动数据文件 1.解压5.7.10包到/usr/local2.停止当前的 ...

  8. ZOJ3329One Person Game(循环型 数学期望)

    There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. ...

  9. Jsp邮件找回密码全攻略

    [来源网络  http://www.2cto.com/kf/201502/376374.html] 一般大型网站我们登录的时候,密码忘了都有个功能可以找回密码. 细数下大致的方法: 1.直接把密码发送 ...

  10. Hive中日期处理

    1.日期函数UNIX时间戳转日期函数:from_unixtime() 函数 格式 返回值 说明 from_unixtime from_unixtime(bigint unixtime[, string ...