随笔记:如何使用Python连接(/操作)Oracle数据库(Windows平台下)
遇到需求,我们需要用Python对Oracle数据库进行操作。
这次我们使用cx_Oracle
Oracle Client
在安装cx_Oracle之前,先安装Oracle客户端。
cx_Oracle
cx_Oracle是一个遵循Python数据库API接口的扩展模块,可通过她对Oracle进行操作。
目前,可从此地址下载:http://cx-oracle.sourceforge.net/
我下载的是针对Python2.7、Oracle11g、Win32的版本:cx_Oracle-5.1.3-11g.win32-py2.7.exe (md5)
其安装过程如一般软件。
在代码中即可引入cx_Oracle对数据库进行操作。如以下代码:
查询数据
#coding=utf-8
#!/usr/bin/python
import cx_Oracle; conn = None;
cursor = None;
try:
conn = cx_Oracle.connect('userid/password@xx.xx.xx.xx/sid');
cursor = conn.cursor();
cursor.execute('select t.empno, t.ename from scott.emp t');
# 取回的是列表,列表中包含元组
list = cursor.fetchall();
print list; for record in list:
print "Record %d is %s!" % (record[0], record[1]); except Exception as e:
print ('Error : {}'.format(e));
finally:
cursor.close;
print 'cursor closed';
conn.close;
print 'connection closed';
插入数据
#coding=utf-8
#!/usr/bin/python
import cx_Oracle;
import time; conn = None;
cursor = None;
try:
conn = cx_Oracle.connect('userid/password@xx.xx.xx.xx/sid');
cursor = conn.cursor();
tuple = ('', 'Nick Huang');
cursor.execute("insert into scott.emp (empno, ename) values (:1, :2)", tuple);
conn.commit();
print 'Insert successfully.'; except Exception as e:
print ('Error : {}'.format(e));
finally:
cursor.close;
print 'cursor closed';
conn.close;
print 'connection closed';
更新数据
#coding=utf-8
#!/usr/bin/python
import cx_Oracle;
import time; conn = None;
cursor = None;
try:
conn = cx_Oracle.connect('userid/password@xx.xx.xx.xx/sid');
cursor = conn.cursor();
tuple = ('Robin Chen', '');
cursor.execute("update scott.emp t set t.ename = :1 where t.empno = :2", tuple);
conn.commit();
print 'Update successfully.'; except Exception as e:
print ('Error : {}'.format(e));
finally:
cursor.close;
print 'cursor closed';
conn.close;
print 'connection closed';
删除数据
#coding=utf-8
#!/usr/bin/python
import cx_Oracle;
import time; conn = None;
cursor = None;
try:
conn = cx_Oracle.connect('userid/password@xx.xx.xx.xx/sid');
cursor = conn.cursor();
param_map = {'id' : ''};
cursor.execute("delete from scott.emp t where t.empno = :id", param_map);
conn.commit();
print 'Delete successfully.'; except Exception as e:
print ('Error : {}'.format(e));
finally:
cursor.close;
print 'cursor closed';
conn.close;
print 'connection closed';
崎岖
1、运行时,报如下异常,发现是Oracle客户端与cx_Oracle版本不一致导致的。
我的Oracle客户端安装的是10g,cx_Oracle是11g的,我重新安装了10g的cx_Oracle就可以了。
D:\python27_workspace>"019.edit oracle.py"
Error : ORA-24315: illegal attribute type
2、报如下异常,一般为cx_Oracle不支持此种参数绑定方式
Error : ORA-01036: illegal variable name/number
如下面方式绑定一个参数,不知为何帮上述异常,后来换成元组的形式绑定参数,就OK了
#coding=utf-8
#!/usr/bin/python
import cx_Oracle;
import time; conn = None;
cursor = None;
try:
conn = cx_Oracle.connect('apps/apps@192.168.0.206/PROD');
cursor = conn.cursor();
tuple = ('');
cursor.execute("delete from scott.emp t where t.empno = :1", tuple);
conn.commit();
print 'Delete successfully.'; except Exception as e:
print ('Error : {}'.format(e));
finally:
cursor.close;
print 'cursor closed';
conn.close;
print 'connection closed';
参考:
http://blog.csdn.net/kongxx/article/details/7107661
http://evil850209.iteye.com/blog/1394932
随笔记:如何使用Python连接(/操作)Oracle数据库(Windows平台下)的更多相关文章
- Python使用cx_Oracle模块连接操作Oracle数据库
1. 简单介绍 cx_Oracle 是一个用来连接并操作 Oracle 数据库的 Python 扩展模块, 支持包含 Oracle 9.2 10.2 以及 11.1 等版本号 2.安装 最好是去官网h ...
- dos命令行连接操作ORACLE数据库
C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...
- python 连接操作 各类数据库
转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...
- python 连接操作mysql数据库
开发数据库程序流程: 1.创建connection对象,获取cursor 2.使用cursor执行SQL 3.使用cursor获取数据.判断执行状态 4.提交事务 或者 回滚事务 import: 数据 ...
- Python操作Oracle数据库:cx_Oracle
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库
前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...
- python操作oracle数据库-查询
python操作oracle数据库-查询 参照文档 http://www.oracle.com/technetwork/cn/articles/dsl/mastering-oracle-python- ...
- python接口自动化测试框架实现之操作oracle数据库
python操作oracle数据库需要使用到cx-oracle库. 安装:pip install cx-oracle python连接oracle数据库分以下步骤: 1.与oracle建立连接: 2. ...
- Java java jdbc thin远程连接并操作Oracle数据库
JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...
- 连接Linux服务器操作Oracle数据库
连接Linux服务器操作Oracle数据库 由于项目已经上线,现场的数据库服务器不允许直接用Oracle的客户端plsqldev.exe来连接,只能通过Linux服务器的命令来操作. 以下是用Se ...
随机推荐
- jsp&html页面知识点集锦
CreateTime--2016年12月16日16:08:03Author:Marydonjsp&html页面知识点集锦1.标签的class属性 标签同时拥有多个class时,要写在同一个 ...
- Linux应用小技巧
简介 本文针对Linux操作过程中提升工作效率问题,给出常见操作技巧,主要从Linux终端管理.显示git分支.终端快速检索历史命令等方面进行介绍. 本文内容主要以Ubuntu系统为例进行介绍. Li ...
- js:获取节点相关的 nodeName,nodeType,nodeValue
getElementById() getElementsByName() getElementsByTagName() hasChildNodes() nodeName nodeType=1元素节点/ ...
- HDUOJ--8球胜负
8球胜负 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 使用springboot遇到的的异常
Unregistering JMX-exposed beans on shutdown <dependency> <groupId>org.springframework.bo ...
- 配置Eclipse 3.3 + tomcat 6.0 + lomboz 3.3进行Web开发
http://www.cnblogs.com/xtsong/articles/1203155.html我以前编程用的都是Eclipse 3.2,这次跑到www.eclipse.org上去溜达了一番,发 ...
- SQL Server 2005/2008遍历所有表更新统计信息
DECLARE UpdateStatisticsTables CURSOR READ_ONLY FOR 02 SELECT sst.name, 03 Schema_name(ss ...
- 什么是FSK制式?什么是DTMF制式?
目前国内来电显示制式有FSK.DTMF(双音频)两种,普通推广的是FSK."来电显示"又称"主叫号码显示"(Calling Identity Delivery) ...
- Spring +quartz获取ApplicationContext上下文
job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...
- Python监控Windows下的文件变化
windows下监控文件系统的变化.用python非常方便.实例代码例如以下,非常easy.也不多说了. import os import win32file import win32con ACTI ...