我对 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/