設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 511|回復: 0
打印 上一主題 下一主題

Calling PL/SQL Procedures in Python

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2022-4-11 21:41:10 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
Setting up a PL/SQL procedureThe following statement creates a new procedure called get_order_count() that returns the number of sales orders by a salesman in a specific year.
  1. CREATE OR REPLACE PROCEDURE get_order_count(
  2.     salesman_code NUMBER,
  3.     year NUMBER,
  4.     order_count OUT NUMBER)
  5. IS     
  6. BEGIN     
  7.     SELECT
  8.         COUNT(*) INTO order_count  
  9.     FROM orders
  10.     WHERE salesman_id = salesman_code AND
  11.         EXTRACT(YEAR FROM order_date) = year;
  12. EXCEPTION
  13.     WHEN OTHERS THEN
  14.         dbms_output.put_line(sqlerrm);
  15. END;        
複製代碼
To test the procedure, you can use the following code using SQL*Plus or SQL Developer:
  1. SET SERVEROUTPUT ON;

  2. DECLARE
  3.     l_order_count NUMBER;
  4. BEGIN
  5.     get_order_count(54,2017,l_order_count);
  6.     dbms_output.put_line(l_order_count);
  7. END;   
複製代碼
The output of the code is:
  1. 3
複製代碼

Calling a PL/SQL procedure in Python example


To execute a PL/SQL procedure, you use the Cursor.callproc() method.
The following code illustrates how to call the procedure get_order_count() and print out the number of orders of the salesman 54 in 2017:
  1. import cx_Oracle
  2. import config as cfg


  3. def get_order_count(salesman_id, year):
  4.     """
  5.     Get order count by salesman and year
  6.     :param salesman_id:
  7.     :param year:
  8.     :return: the number of orders by a salesman and year
  9.     """
  10.     try:
  11.         # create a connection to the Oracle Database
  12.         with cx_Oracle.connect(cfg.username,
  13.                             cfg.password,
  14.                             cfg.dsn,
  15.                             encoding=cfg.encoding) as connection:
  16.             # create a new cursor
  17.             with connection.cursor() as cursor:
  18.                 # create a new variable to hold the value of the
  19.                 # OUT parameter
  20.                 order_count = cursor.var(int)
  21.                 # call the stored procedure
  22.                 cursor.callproc('get_order_count',
  23.                                 [salesman_id, year, order_count])
  24.                 return order_count.getvalue()
  25.     except cx_Oracle.Error as error:
  26.         print(error)


  27. if __name__ == '__main__':
  28.     orders = get_order_count(54, 2017)
  29.     print(orders)  # 3
複製代碼
Note that the config.py is as follows:
  1. username = 'OT'
  2. password = '<password>'
  3. dsn = 'localhost/pdborcl'
  4. port = 1512
  5. encoding = 'UTF-8'
複製代碼

In this example:
First, connect to the Oracle Database by calling the cx_Oracle.connect() method with the parameters provided by the config module.
Second, create a new Cursor object by calling the Connection.cursor() method.
Third, create a new variable that will hold the returned value of the OUT parameter of the procedure:
  1. order_count = cursor.var(int)
複製代碼
Fourth, call the procedure get_order_count() using the Cursor.callproc() method:
  1. cursor.callproc('get_order_count', [salesman_id, year, order_count])
複製代碼
Finally, call the Variable.getvalue() method to return the value of the variable.
  1. return order_count.getvalue()
複製代碼

Because we used the with block, the Cursor and Connection objects were automatically released.
It is important to note that when you call the Cursor.callproc() method, cx_Oracle actually generates the following anonymous block and then executes it:
  1. cursor.execute("begin get_order_count(:1,:2,:3); end;", [salesman_id, year, order_count])
複製代碼









分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-17 00:31 , Processed in 0.327339 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表