首页 > 编程 > Python > 正文

Python使用cx_Oracle调用Oracle存储过程的方法示例

2019-11-25 15:48:42
字体:
来源:转载
供稿:网友

本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下:

这里主要测试在Python中通过cx_Oracle调用PL/SQL。

首先,在数据库端创建简单的存储过程。

create or replace procedure test_msg(i_user in varchar2, o_msg out varchar2) isbegin o_msg := i_user ||', Good Morning!';end;

然后,开始在Python命令行中进行存储过程调用。

import cx_Oracle as cxconn = cx.connect('database connecting string')cursor = conn.cursor()#声明变量user = 'Nick' #plsql入参msg = cursor.var(cx_Oracle.STRING) #plsql出参#调用存储过程cursor.callproc('test_msg', [user, msg]) #['Nick', 'Nick, Good Morning!']#打印返回值print msg #<cx_Oracle.STRING with value 'Nick, Good Morning!'>print msg.getvalue() #Nick, Good Morning!#资源关闭cursor.close()conn.close()

延伸阅读:

存储过程、cx_Oracle、Python的对象类型之间存在转换关系。具体如下:

Oracle cx_Oracle Python
VARCHAR2, NVARCHAR2, LONG cx_Oracle.STRING str
CHAR cx_Oracle.FIXED_CHAR str
NUMBER cx_Oracle.NUMBER int
FLOAT cx_Oracle.NUMBER float
DATE cx_Oracle.DATETIME datetime.datetime
TIMESTAMP cx_Oracle.TIMESTAMP datetime.datetime
CLOB cx_Oracle.CLOB cx_Oracle.LOB
BLOB cx_Oracle.BLOB cx_Oracle.LOB

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表