简体   繁体   English

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

[英]Executing script by using function callproc from cx_Oracle module in python 2.7.5

I'm relatively new to Python. 我对Python比较陌生。 I'm currently working on SQL statement execution in Oracle DB. 我目前正在研究Oracle DB中的SQL语句执行。

When I execute query: 当我执行查询时:

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

everything is going fine, but when I try to execute script: 一切都很顺利,但是当我尝试执行脚本时:

script in plain text: 纯文本脚本:

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

code from script 来自脚本的代码

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

result = cursor.fetchall()

I get an exception, that this is not a query, but still this script has worked. 我得到一个例外,这不是一个查询,但这个脚本仍然有效。

So from what I've googled, looks like I should use callproc function: 所以从我用Google搜索,看起来我应该使用callproc函数:

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

result = cursor.fetchall()

When I'm executing this statement, I'm also getting exception, but this time nothing has been changed in DB: 当我执行此语句时,我也遇到异常,但这次DB中没有任何更改:

'builtin_function_or_method' object has no attribute ' getitem ' 'builtin_function_or_method'对象没有属性' getitem '

Could someone please point where I'm not correct and how should I modify statement so it would be working. 有人可以指出我不正确的地方,我应该如何修改声明,以便它能正常工作。

Huge thanks in advance! 非常感谢提前!

RESOLUTION: 解析度:

I was frustrated by the syntax and the complexity of callproc and callfunc functions. 我对callproc和callfunc函数的语法和复杂性感到沮丧。 I've found good resource: http://dbaportal.eu/sidekicks/sidekick-cx_oracle-code-paterns/#part1 in this link I found all needed info and examples on how to work with cx_Oracle library. 我找到了很好的资源: http//dbaportal.eu/sidekicks/sidekick-cx_oracle-code-paterns/#part1在这个链接中我发现了所有需要的信息和如何使用cx_Oracle库的例子。

at the end I just needed to modify a bit my code: 最后我只需要修改一下我的代码:

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

and the needed part was done, I didn't need to specify any return type, as script that I'm executing doesn't return any value, it just sets it. 并且所需的部分已完成,我不需要指定任何返回类型,因为我正在执行的脚本不返回任何值,它只是设置它。

The exception is because you are using [ ] where you should be using () : 例外情况是因为你正在使用[ ]你应该使用的地方()

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

Keep in mind the return type is required: 请记住,返回类型是必需的:

Cursor.callfunc(name, returnType, parameters=[], keywordParameters = {}) 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(). 返回类型以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. 调用的结果是函数的返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM