zoukankan      html  css  js  c++  java
  • python cx_Oracle模块的安装和使用(linux环境)

    安装:

    1. 首先确定版本。因为我们的Oracle数据是在是有点老,所以我选择了一个比较老的版本——Oracle Instant Client 10.2.0.4。

    2. 下载instantclient-basic。下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

    3.下载instantclient-sdk,这个是必须的。

    $unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
    $unzip instantclient-sdk-linux-x86-64-10....zip
    $cd instantclient_10_2
    $cp * /usr/lib   #直接放到动态库搜索路径中,不需要额外的环境配置
    
    或
    $unzip instantclient-basic-linux.x64-10.2.0.4.0.zip
    $unzip instantclient-sdk-linux-x86-64-10....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

         Ubuntu 不能在 /etc/profile, environment,.bashrc中设置 LD_LIBRARY_PATH;
    解决办法:
         编辑 /etc/ld.so.conf 文件,将指定的路径加上,或者在 /etc/ld.so.conf.d/ 目录中添加一个新的配置文件。

    3.下载安装cx_Oracle python模块

    tar zxvf cx_Oracle-5.1.tar.gz
    cd cx_Oracle-5.1
    python setup.py install
    

    4.验证及问题解决

    使用:

    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种连接方式,瞬间解决此问题。

     可能出现的问题:

    /usr/bin/ld: cannot find lclntsh

    找不到这个lib文件。

    解决方法:

    到/opt/instantclient_10_2目录下,修改:

    ln -s libclntsh.so.10.1  libclntsh.so


     

  • 相关阅读:
    人工大脑项目 —— Nengo
    四种聚类方法之比较
    对淘宝一些规则的一些研究分享
    【转】千万不要在夏季开发苹果应用,否则后果很严重
    Windows2003上使用IIS7 Express使用FastCgi运行php
    傅立叶变换最直白最容易理解最直接最真实最有深度的解释
    音视频技术应用(18) 控制播放进度——av_seek_frame()
    MySQL 中如何定位 DDL 被阻塞的问题
    windows 编译版本异常处理
    R语言的前世今生
  • 原文地址:https://www.cnblogs.com/chenjianhong/p/4144770.html
Copyright © 2011-2022 走看看