我正在尝试使用python中的cx_Oracle连接到oracle数据库。我能够连接到数据库并提取数据。
现在,我尝试连接到另一个数据库,在该数据库中,我必须先调用安全性过程,然后才能看到基础表中的数据。您能否通过调整以下代码块来帮助我调用此过程。我的程序将类似于:exec ef.applogin('1234')
def connect_oracle():
import cx_Oracle
import pandas as pd
ip = 'some_ip'
port = 1521
SID = 'some_SID'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
connection = cx_Oracle.connect('user_name', 'password', dsn_tns)
#Procedure to be executed here..
query = """ SELECT * from table_name """
df_ora = pd.read_sql(query, con=connection)
return df_ora
connect_oracle()
非常感谢!
答案 0 :(得分:1)
创建def以供Oracle连接。对于执行过程,请使用游标。不要忘记关闭连接和光标。同样以防万一。
import cx_Oracle
import pandas as pd
ip = 'some_ip'
port = 1521
SID = 'some_SID'
user_name = 'user_name'
password = 'password'
query = """ SELECT * from table_name """
def connect_oracle(ip, port, SID, user_name, password):
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
connection = cx_Oracle.connect(user_name, password, dsn_tns)
return connection
def get_df(conn, query):
df_ora = pd.read_sql(query, con=conn)
return df_ora
def execute_proc(conn, procname, param):
my_cursor=conn.cursor()
my_cursor.callproc(procname, param)
conn.commit()
def execute_func(conn, procname, param):
my_cursor=conn.cursor()
out_parameter = my_cursor.var(cx_Oracle.NUMBER)
qw = my_cursor.callfunc(procname, out_parameter)
return qw
conn1 = connect_oracle(ip, port, SID, user_name, password)
df = get_df(conn1, query)
df.head()
execute_proc(conn1, "insert_temp", [])
execute_func(conn1, "get_version", [])
测试功能
create or replace function get_version
return number
is
v_version NUMBER(8) := 11;
begin
return v_version;
end;
测试步骤:
create table temp (clm number);
create or replace procedure insert_temp
is
v_version NUMBER(8) := 11;
begin
insert into temp(clm) values(v_version);
end;
select * from temp