簡體   English   中英

如何使用 Python 讀取 Oracle 存儲過程的內容

[英]How to i read the contents of Oracle stored procedures using Python

我正在嘗試使用 python 讀取存儲過程的內容/代碼。

我使用 cx_Oracle 函數來建立與 oracle 數據庫的連接。

這是代碼

import cx_Oracle as co
import pandas as pd

dsn_tsn = co.makedsn(ip,port,SID)
db=co.connect(username,password,dsn_tsn)
cursor = db.cursor()

cursor.callproc(procedure_name,['argument']) # will be me result of the procedure.

但是,我正在嘗試閱讀程序本身的代碼。 有什么功能可以做到這一點嗎?

可以通過user_source視圖訪問存儲過程的代碼。 所以,如果你查詢它,你會看到你想要的。 就是這樣:

SQL> create or replace procedure p_test is
  2  begin
  3    null;
  4  end;
  5  /

Procedure created.

SQL> desc user_source
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 NAME                                               VARCHAR2(30)
 TYPE                                               VARCHAR2(12)
 LINE                                               NUMBER
 TEXT                                               VARCHAR2(4000)

SQL> select text from user_source where name = 'P_TEST' and type = 'PROCEDURE';

TEXT
--------------------------------------------------------------------------------
procedure p_test is
begin
  null;
end;

SQL>

不過,由於我不會說 Python,我無法幫助您編寫需要在那里使用的實際代碼。 我寫的最后一個select是你需要的; 我希望你會設法使用它。 祝你好運!

您可以通過這種方式從代碼中調用DBMS_METADATA.GET_DDL函數

import cx_Oracle

db = cx_Oracle.connect("<uname>/<pwd>@<host>:<port>/<service_name>")
cursor = db.cursor()


    def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
        if defaultType == cx_Oracle.CLOB:
            return cursor.var(cx_Oracle.LONG_STRING, arraysize = cursor.arraysize)

    cursor.outputtypehandler = OutputTypeHandler
    cursor.execute("SELECT DBMS_METADATA.GET_DDL('PROCEDURE', :PrcName) FROM DUAL",
            PrcName="MY_LITTLE_PROC")

    print("returned DDL is :",cursor.fetchall())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM