遇到需求,我们需要用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平台下)的更多相关文章

  1. Python使用cx_Oracle模块连接操作Oracle数据库

    1. 简单介绍 cx_Oracle 是一个用来连接并操作 Oracle 数据库的 Python 扩展模块, 支持包含 Oracle 9.2 10.2 以及 11.1 等版本号 2.安装 最好是去官网h ...

  2. dos命令行连接操作ORACLE数据库

    C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...

  3. python 连接操作 各类数据库

    转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...

  4. python 连接操作mysql数据库

    开发数据库程序流程: 1.创建connection对象,获取cursor 2.使用cursor执行SQL 3.使用cursor获取数据.判断执行状态 4.提交事务 或者 回滚事务 import: 数据 ...

  5. Python操作Oracle数据库:cx_Oracle

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  6. 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库

    前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...

  7. python操作oracle数据库-查询

    python操作oracle数据库-查询 参照文档 http://www.oracle.com/technetwork/cn/articles/dsl/mastering-oracle-python- ...

  8. python接口自动化测试框架实现之操作oracle数据库

    python操作oracle数据库需要使用到cx-oracle库. 安装:pip install cx-oracle python连接oracle数据库分以下步骤: 1.与oracle建立连接: 2. ...

  9. Java java jdbc thin远程连接并操作Oracle数据库

    JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...

  10. 连接Linux服务器操作Oracle数据库

    连接Linux服务器操作Oracle数据库   由于项目已经上线,现场的数据库服务器不允许直接用Oracle的客户端plsqldev.exe来连接,只能通过Linux服务器的命令来操作. 以下是用Se ...

随机推荐

  1. ehcache.xml的配置详解和示例

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...

  2. git的color configura

    git color的配置 Git多颜色输出 Git默认的输出是单一颜色的,不仅不够美观,也不容易阅读.实际上,Git本身就支持用多种颜色来显示其输出的信息,只需在命令行中运行以下命令来修改git的设置 ...

  3. Ubuntu解压缩zip,tar,tar.gz,tar.bz2

    ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如Linux, Windows以及Mac OS,上使用.缺点就是支持的压缩率不是很高,而tar.gz和tar. ...

  4. 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构

    作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell [ ...

  5. python之函数用法locals()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法locals() #locals() #说明:查找局部变量,返回一个名字/值对的字典对 ...

  6. from会存在潜在的陷阱

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #from会存在潜在的陷阱 #from时,可能会遇到相同变量名,变量会被悄悄覆盖掉, #但是import语句不 ...

  7. Q1:Valid Parentheses

    Question: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine i ...

  8. 如何架设部署V2EX社区/论坛(Google App Engine版)

    1.What's V2EX? 关于这个问题,我们可以看看其作者Livid早期自己的V2EX社区的介绍: What's V2EX? 这是很多人都问过的问题,而我一直都没有做出一个明确的解答.因为我实在觉 ...

  9. PHP扩展的基本结构

    1.下载php源码 git clone https://github.com/php/php-src.git  2,创建扩展 cd php-src/ext/ ./ext_skel --extname= ...

  10. LNMP的的编译安装全过程

    一.对系统进行更新 yum update -y lsb_release -a 二.禁用SELINUX sed -i '/SELINUX/s/enforcing/disabled/' /etc/seli ...