System Environment:
[Root @ nich4 cx_Oracle-py] # uname-
Linux nich4 2.6.18-164. el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
Python version:
[Root @ nich4 cx_Oracle-py] # python-V
Python 2.4.3
Oracle version:
Oracle database 10.2.0.5 x86
Cx_Oracle introduction:
Cx_Oracle is a Python extension module that queries and updates Oracle databases by using common database APIs of all database access modules. In order to use some features designed for Oracle, www.bkjia.com has also added extensions for multiple common database APIs.
Cx_Oracle has been developed for more than 10 years, covering the needs of most customers who need to access Oracle in Python. In December 2008, a new major version solved many restrictions in earlier versions and added support for some new features of Python 3.0 and Oracle.
Cx_Oracle official and download: http://cx-oracle.sourceforge.net/
Download the cx_Oracle version corresponding to the python version. Generally, select a non-UNICODE version.
I chose CentOS 5 i386 RPM (Oracle 10g, Python 2.4)
Install rpm-ivh after the download.
Modify oracle environment variables and add:
Export LD_LIBRARY_PATH =/opt/oracle/10g \:/opt/oracle/10g/network/lib
Database Connection example:
The following is a script for Hot Backup of data files in an archive database.
#! /Bin/env python
#-*-Coding: UTF-8 -*-
# Modified: 20101012-23:25:43 by nich4@msn.com
# Filename: hotbak. py
#
Import subprocess
Import cx_Oracle
Import OS
Db_username = 'sys'
Db_passwd = 'oracle123'
Db_sid = 'sol10g'
Bak_dir = '/u01/app/oracle/oradata/bak/sol10g /'
Db = cx_Oracle.connect (db_username, db_passwd, db_sid, cx_Oracle.SYSDBA)
Print "connected to the database! "
Curs = db. cursor ()
SQL = r''' select 'cp' | name | '% s' from v $ datafile ''' % bak_dir
Res = []
For rs in curs.exe cute (SQL ):
Res. append (rs [0])
Curs.exe cute ('alter database begin backup ')
Print "start backing up data files ..."
For I in res:
Fname = I. split () [1]. split ('/') [-1]. strip ()
Print "backing up % s to % s..." % (fname, bak_dir)
Subprocess. call (I, shell = True)
Print "data file % s backup completed! "% Fname
Curs.exe cute ('alter database end backup ')
Db. close ()
Print "backup complete! "
Note: This script is tested in solaris-10 + oracle-10gR2. This script does not use python's shutil module to execute a copy of the file. for demonstration purposes only.