Tag Archives: cx_Oracle

Python连接oracle的几种方式

  1. 基本连接–使用Oracle tns alias
    connection = cx_Oracle.connect("tp/tp@ocn_test")
    
    #查看tns alias命令
    cmd>tnsping ocn_test 
    TNS Ping Utility for Linux: Version 9.2.0.8.0 - Production on 27-SEP-2011 10:47:48
    Copyright (c) 1997, 2006, Oracle Corporation.  All rights 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 (10 msec)
    
  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)
    

 

Python&&cx_Oracle

Python版本:python2.7.2
Instantclient版本:Version 10.2.0.4
cx_Oracle版本:cx_Oracle-5.0.1
下载软件及工程目录:/home/oracle/LL/python/software(/workstation)

1. 查看系统版本选择合适版本
uname -a
Linux inc-dba-ccbu-36-18 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux

2. 下载安装python2.7.2 – 稳定版
# wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tar.bz2
# tar -jxvf Python-2.7.1.tar.bz2
# cd Python-2.7.1
# ./configure (默认安装在/usr/local/lib/python2.7,–prefix可指定)
# make && make install

Continue reading