简体   繁体   English

如何使用cx_Oracle运行非查询sql命令?

[英]How do I run non query sql commands using cx_Oracle?

I am trying to run these commands using cx_oracle: 我正在尝试使用cx_oracle运行以下命令:

begin
add_command_pkg.add_command
(  command_id    => 7,
   expiry_time   => sysdate + 7
 );

add_command_pkg.add_command
(  command_id    => 12,
   expiry_time   => sysdate + 7
 );
commit;
end;

So this is my Python code: 这是我的Python代码:

dsn = cx_Oracle.makedsn(hostname, port, sid)
orcl = cx_Oracle.connect(username + '/' + password + '@' + dsn)

curs = orcl.cursor()
cmd = "begin \n\
       add_command_pkg.add_command \n\
       (  command_id    => 7, \n\
          expiry_time   => sysdate + 7 \n\
       ); \n\
       \n\
       add_command_pkg.add_command \n\
       (  command_id    => 12, \n\
          expiry_time   => sysdate + 7 \n\
       ); \n\
       commit; \n\
       end;"

curs.execute(cmd)
orcl.close()

When I run this code, I get this error: 运行此代码时,出现以下错误:

cx_Oracle.InterfaceError: not a query cx_Oracle.InterfaceError:不是查询

So how do I run these sql commands that aren't queries using cx_oracle? 那么,如何使用cx_oracle运行不是查询的这些sql命令?

Edit: 编辑:

After making changes this is what I have now: 进行更改后,这就是我现在所拥有的:

            curs.callproc('add_command_pkg.add_command', [],
                          { 'command_id' : 7,
                            'session_id' : 'null',
                            'p_expiry_time' : 'sysdate + 7',
                            'config_id' : 6 })

When I run this, I get this error: 运行此命令时,出现以下错误:

File "N:\\App\\MainWidget.py", line 456, in myFunc myFunc中的文件“ N:\\ App \\ MainWidget.py”,行456
'config_id' : 6 }) 'config_id':6})
cx_Oracle.DatabaseError: ORA-01858: a non-numeric character was found where a numeric was expected cx_Oracle.DatabaseError:ORA-01858:在需要数字的位置找到了非数字字符
ORA-06512: at line 1 ORA-06512:在第1行

Also, how do I commit this? 另外,我该如何提交?

The best way is to call the procedure directly using callproc . 最好的方法是直接使用callproc调用过程。

curs.callproc['add_command_pkg.add_command',['7', 'sysdate + 7']]
orcl.commit()

or if you need to use keyword arguments directly use a dictionary not a list. 或者,如果您需要直接使用关键字参数,请使用字典而不是列表。

curs.callproc['add_command_pkg.add_command'
             , {'command_id' : '7', 'expiry_time' : 'sysdate + 7'}]
orcl.commit()

The actual syntax is 实际的语法是

curs.callproc['package_name.procedure_name'
             , ['list_argument1', 'list_argument2']
             , {'keyword_argument1' : 'keyword1'}
             ]

Which is the same as the following in Oracle 与Oracle中的以下内容相同

begin
    package_name.procedure_name( 'list_argument1', 'list_argument2'
                               , keywork_argument1 => 'keyword1');
end;

Whilst I'm about the connect method can be called in the following way without the need for concatenation: 虽然我说的是connect方法,但可以通过以下方式调用,而无需进行串联:

 cx_Oracle.connect(username, password, dsn)

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

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