随笔记:如何使用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 ...
随机推荐
- 优化后队列的实现(C语言实现)
上一篇中的队列的定义与实现(C语言实现) 中.不管是顺序队列还是链式队列,在尾加和删除头部的操作时.总有一个时间复杂度让人不惬意. 比方在顺序队列中,删除头部的操作后,总要将后面全部的结点都向前移动一 ...
- 整理两个JVM博客集合,空闲时候可以看
纯洁的微笑写的:https://www.cnblogs.com/ityouknow/p/5614961.html 集合:http://www.cnblogs.com/ityouknow/categor ...
- shell随机读取一行
使用shell随机读取文件的一行数据 shuf -n1 file_name
- Spring配置文件头信息
代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- PAT 1087 All Roads Lead to Rome
PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...
- 自定义类似于listView中Item背景
方法一. drawable/listitem_bk.xml <?xml version="1.0" encoding="utf-8" ?> < ...
- iOS “[App] if we're in the real pre-commit handler we can't actually add any new fences due
最近运行APP,发现了这个问题,本着宁可错看,不可放过的原则,上stackoverFlow学习了一下: 链接:http://stackoverflow.com/questions/38458170/i ...
- 消息队列实现回射客户/服务器和 msgsnd、msgrcv 函数
一.msgsnd 和 msgrcv 函数 #include <sys/types.h> #include <sys/ipc.h> #include <sys/ms ...
- yield与send实现协程操作
yield与send实现协程操作 之前我们说过,在函数内部含有yield语句即称为生成器. 下面,我们来看看在函数内部含有yield语句达到的效果.首先,我们来看看以下代码: def foo(): w ...
- Python 列表 pop() 方法
描述 Python 列表 pop() 方法通过指定元素的索引值来移除列表中的某个元素(默认是最后一个元素),并且返回该元素的值,如果列表为空或者索引值超出范围会报一个异常. 语法 pop() 方法语法 ...