I'm relatively new to Python. I'm currently working on SQL statement execution in Oracle DB.
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:
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:
'builtin_function_or_method' object has no attribute '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. 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.
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.
To call this procedure from Python, I use the cursor. callproc method and pass in the package. procedure name to execute it. the code is successful and does not return a response.
cx_Oracle is a Python extension module that enables access to Oracle Database. It conforms to the Python database API 2.0 specification with a considerable number of additions and a couple of exclusions. cx_Oracle 8.3 was tested with Python versions 3.6 through 3.10.
Build with Python on Oracle Cloud. Intuitive and powerful, Python is the perfect match for OCI. Check out these resources to get started on your next ML project, build an app, or extend the functionality of your cloud instance.
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 = {})
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With