python - 在 python 2.7.5 中使用来自 cx_Oracle 模块的函数 callproc 执行脚本

标签 python sql cx-oracle

我对 Python 比较陌生。 我目前正在 Oracle DB 中执行 SQL 语句。

当我执行查询时:

query = 'select * from table' 
cursor.execute(query)
result = cursor.fetchall()

一切正常,但是当我尝试执行脚本时:

纯文本脚本:

begin
 SIEBEL_DBA.X_DR_DEPLOY(id => '1-4NANEI', env_code => 'SVE_SIT');
end;
/

脚本中的代码

script = "begin\nSIEBEL_DBA.X_DR_DEPLOY(id => '1-4NANEI', env_code => 'SVE_SIT');\nend;"
cursor.execute(script)

result = cursor.fetchall()

我得到一个异常,这不是一个查询,但这个脚本仍然有效。

所以从我用谷歌搜索的结果来看,我应该使用 callproc 函数:

cursor.callproc['SIEBEL_DBA.X_DR_DEPLOY',{'id' : '1-4NANEI', 'env_code' : 'SVE_SIT'}]
connection.commit()

result = cursor.fetchall()

当我执行这条语句时,我也遇到了异常,但是这次数据库中没有任何改变:

“builtin_function_or_method”对象没有属性“getitem

有人可以指出我不正确的地方吗?我应该如何修改声明才能正常工作。

非常感谢!

解决方案:

我对 callproc 和 callfunc 函数的语法和复杂性感到沮丧。 我找到了很好的资源:http://dbaportal.eu/sidekicks/sidekick-cx_oracle-code-paterns/#part1 在此链接中,我找到了有关如何使用 cx_Oracle 库的所有需要​​的信息和示例。

最后我只需要修改一下我的代码:

cursor.callproc('SIEBEL_DBA.X_DR_DEPLOY', ['1-4NANEI', 'SVE_SIT'])

所需的部分已经完成,我不需要指定任何返回类型,因为我正在执行的脚本不会返回任何值,它只是设置它。

最佳答案

异常是因为您在应该使用 () 的地方使用了 [ ]:

cursor.callproc('SIEBEL_DBA.X_DR_DEPLOY',{'id' : '1-4NANEI', 'env_code' : 'SVE_SIT'})

请记住返回类型是必需的:

Cursor.callfunc(name, returnType, parameters=[], keywordParameters = {})

Call a function with the given name. The return type is specified in the same notation as is required by setinputsizes(). The sequence of parameters must contain one entry for each argument that the function expects. Any keyword parameters will be included after the positional parameters. The result of the call is the return value of the function.

关于python - 在 python 2.7.5 中使用来自 cx_Oracle 模块的函数 callproc 执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21478484/

相关文章:

python - cx_Oracle 忽略 order by 子句

python - Windows 上没有适用于 Python 3.5 的 cx_Oracle 吗?

python - 即使使用信号处理程序,数据库调用也会中断

python - PyQt : How to create a lineItem between two button and can move with button?

python - 无法通过具有指定 device_id 的 pylibftdi 与 FTDI 设备通信

sql - 在 Oracle 上选择字符串作为数字

sql - 在带有 PK 标识列的表中插入期间出现 AS400 DB2 重复键错误

python - Tkinter 将 optionMenu 中选择的选项放入变量中以供进一步使用

python - 皮克瓦利 : Validate data in a dictionary against a yaml file schema

sql - PostgreSQL 序列线程上的 setval nextval 增量是否安全?