Mic*_*cht 7 python plsql cx-oracle
我想通过Python中的cx_oracle执行Oracle PL/SQL语句.代码如下所示:
db = cx_Oracle.connect(user, pass, dsn_tns)
cursor = db.cursor()
...
sel = """
DECLARE
c NUMBER := 0.2;
mn NUMBER := 1.5;
res NUMBER;
BEGIN
res := c+mn/6.;
END;
"""
try:
cursor.execute(sel)
print "PL/SQL successful executed ..."
except cx_Oracle.DatabaseError as e:
err, = e.args
print "\n".join([str(err.code),err.message,err.context])
Run Code Online (Sandbox Code Playgroud)
代码运行没有问题,但有没有机会将结果返回到Python?
小智 11
您可以像这样将输入和输出变量绑定到块.
import cx_Oracle
SQL_BLOCK = '''
DECLARE
v_first NUMBER;
v_second NUMBER;
v_result NUMBER;
BEGIN
v_first := :i_first; -- (1)
v_second := :i_second; -- (1)
v_result := (v_first + v_second) / 2;
:o_result := v_result; -- (1)
END;
'''
with cx_Oracle.connect('hr/hr@xe') as db:
cur = db.cursor()
o_result = cur.var(cx_Oracle.NUMBER) # (2)
cur.execute(SQL_BLOCK, i_first=23, i_second=55, o_result=o_result) # (3)
res = o_result.getvalue() # (4)
print('Average of 23 and 55 is: {}'.format(res))
Run Code Online (Sandbox Code Playgroud)
该脚本应该打印
Average of 23 and 55 is: 39.0
Run Code Online (Sandbox Code Playgroud)
您需要一个函数来返回结果.匿名块不会.
您需要在数据库中创建一个函数,例如:
create or replace function calculation return number is
c number := 0.2;
mn number := 1.5;
res number;
begin
return c + mn / 6.;
end;
/
Run Code Online (Sandbox Code Playgroud)
然后改变你的Python代码来调用函数,使用, callfunc()
db = cx_Oracle.connect(user, pass, dsn_tns)
cursor = db.cursor()
try:
result = cursor.callfunc('calculation', float)
print result
except cx_Oracle.DatabaseError as e:
err, = e.args
print "\n".join([str(err.code),err.message,err.context])
Run Code Online (Sandbox Code Playgroud)
动态创建函数是不可能的,但是你的函数很简单,你可以在select语句中完成它,并fetchall()按照链接文档中的描述使用它来将结果返回给Python.fetchall()返回一个元组列表,所以如果你只在一行和一列之后,你可以立即选择两者的第 0 个索引.
>>> import cx_Oracle
>>> db = cx_Oracle.connect('****','****','****')
>>> cursor = db.cursor()
>>> SQL = """select 0.2 + 1.5 / 6. from dual"""
>>> try:
... cursor.execute(SQL)
... result = cursor.fetchall()[0][0]
... except cx_Oracle.DataBaseError, e:
... pass
...
<__builtin__.OracleCursor on <cx_Oracle.Connection to ****@****>>
>>> result
0.45000000000000001
>>>
Run Code Online (Sandbox Code Playgroud)
您还可以execute()使用绑定变量将变量传递给调用,并在必要时在Python中实例化它们:
>>> c = 0.2
>>> mn = 1.5
>>> SQL = """select :c + :mn / 6. from dual"""
>>> bind_vars = { 'c' : c, 'mn' : mn }
>>> cursor.execute(SQL, bind_vars)
<__builtin__.OracleCursor on <cx_Oracle.Connection to history@monitor>>
>>> result = cursor.fetchall()[0][0]
>>> result
0.45000000000000001
>>>
Run Code Online (Sandbox Code Playgroud)
虽然在Python中完成所有这些操作可能更简单...我假设您的实际情况更复杂?
| 归档时间: |
|
| 查看次数: |
10748 次 |
| 最近记录: |