Python+cx_Oracle安装及一个简单示例(归档下热备数据文件)
系统环境:[root@nich4 cx_Oracle-py]# uname -aLinux nich4 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i38
系统环境:
[root@nich4 cx_Oracle-py]# uname -a
Linux nich4 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:54 EDT 2009 i686 i686 i386 GNU/Linux
python版本:
[root@nich4 cx_Oracle-py]# python -V
Python 2.4.3
oracle版本:
oracle database 10.2.0.5 x86
cx_Oracle简介:
cx_Oracle 是一个 Python 扩展模块,通过使用所有数据库访问模块通用的数据库 API 来实现 Oracle 数据库的查询和更新。为使用一些专为 Oracle 设计的特性,还加入了多个通用数据库 API 的扩展。
cx_Oracle 的开发历时十多年,涵盖了大多数需要在 Python 中访问 Oracle 的客户的需求。2008 年 12 月,,一个新的主要版本解决了早期版本的众多限制,并增添了对 Python 3.0 和 Oracle 新推出的一些特性的支持。
cx_Oracle官方及下载:
下载与python版本对应的cx_Oracle版本,一般选择非UNICODE版本就行了.
我选择的是CentOS 5 i386 RPM (Oracle 10g, Python 2.4)
下载之后rpm -ivh 安装.
修改oracle环境变量,加入:
export LD_LIBRARY_PATH=/opt/oracle/10g\:/opt/oracle/10g/network/lib
连接数据库示例:
以下是一个在归档数据库下热备份数据文件的脚本.
#!/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 "连接数据库成功!"
curs = db.cursor()
sql = r'''select 'cp '||name||' %s' from v$datafile''' % bak_dir
res = []
for rs in curs.execute(sql):
res.append(rs[0])
curs.execute('alter database begin backup')
print "开始备份数据文件..."
for i in res:
fname = i.split()[1].split('/')[-1].strip()
print "正在备份%s 到 %s ..." % (fname,bak_dir)
subprocess.call(i,shell=True)
print "数据文件%s 备份完成!" % fname
curs.execute('alter database end backup')
db.close()
print "备份完成!"
注: 该脚本在solaris-10+oracle-10gR2下测试. 这个脚本没有使用python的shutil模块来执行文件的拷贝.仅为演示用.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
