最全總結,聊聊 Python 資料處理全家桶(儲存過程篇)

作者:星安果

最全總結,聊聊 Python 資料處理全家桶(儲存過程篇)

1。 前言

大家好,我是安果!

如果專案涉及複雜的 SQL 處理,就可以將這些操作封裝成「 儲存過程 」,公開入參及出參,方便直接呼叫

本篇文章將聊聊如何使用 Python 執行儲存過程

2。 儲存過程

儲存過程,全稱為「

Stored Procedure

可以將它看成一個介面,內部會封裝一些常用的操作,可以直接進行呼叫

儲存過程的常見操作如下:

2-1

管理儲存過程

建立

查詢

刪除

# 1、建立一個儲存過程

# 儲存過程名稱為:xag

delimiter $

create procedure xag()

begin

。。。

end $

# 2。1 透過資料庫名查詢所有儲存過程

# 比如:資料庫名為xag

select `name` from mysql。proc where db = ‘xag’ and `type` = ‘PROCEDURE’;

# 2。2 查詢儲存過程中狀態資訊

show procedure status;

# 3。透過儲存過程名稱,刪除一個儲存過程

DROP PROCEDURE IF EXISTS xag;

其中

使用「 create procedure 儲存過程名稱 」建立一個儲存過程,接著在 begin 和 end 之間編寫具體的操作邏輯

2-2

變數定義及賦值

使用關鍵字「

declare

」可以定義一個變數

# 變數定義

# 比如:定義一個變數name,型別為字串

# 預設值為 null

。。。

declare name varchar(255) default null;

。。。

給變數賦值有 2 種方式:普通 set 語法、select into 語法

其中

set 語法可以透過表示式設定變數的值

select into 語法是透過查詢資料庫表,將查詢結果設定到變數中

# 變數定義

declare name varchar(255) default null;

# 變數賦值

# set語法

set name = ‘xag’;

# select into語法

# 查詢name_table表中的第一條記錄中的name值,儲存到name變數中

select name into name from name_table limit 1;

2-3 條件判斷 if

比如,透過年齡判斷年級( if 語句)

。。。

declare age int default 23;

declare grade varchar(255) default null;

# if語句

if age <=5 then

set grade = ‘幼兒園’;

elseif age >= 6 and age < 12 then

set grade = ‘小學’;

elseif age >=12 and age < 15 then

set grade = ‘初中’;

elseif age >=15 and age < 18 then

set grade = ‘高中’;

elseif age >=18 then

set grade = ‘其他’;

end if;

。。。

2-4

迴圈 while

比如,計算 1-10 數值的和,設定到變數 total 上

。。。

# 總和

declare total int default 0;

# 結束值

declare end_number int default 10;

# 臨時值

declare temp int default 0;

# while迴圈

while temp <= end_number do

# 設定值

set total = total + temp;

set temp = temp + 1;

end while;

。。。

2-5

入參和出參

為了使編寫的儲存過程更加實用,我們需要在常見儲存過程時,設定出參和入參

語法格式如下:

# 建立一個儲存過程

create procedure proce_name([in/out/inout] 引數名 引數型別)

其中

預設傳入值為入參,即 in

out 代表出參,作為返回值返回

如果設定為 inout,則代表既能作為出參,也可以作為入參

3。 實戰一下

使用 Python 呼叫儲存過程非常方便

首先,我們編寫一個儲存過程

比如,我這裡定義了一個儲存過程,傳入兩個入參和一個出參,將兩個入參的乘積作為出參返回

# 定義一個儲存過程

delimiter $

create procedure num_multi(in num1 int,in num2 int,out multiply_result int)

begin

# 兩個入參相乘,然後設定到出參中去

set multiply_result = num1 * num2;

end $

然後,在資料庫中進行呼叫測試

使用關鍵字「 call 」呼叫儲存過程,使用 select 檢視返回值

# 呼叫儲存過程

call num_multi(1,3,@multiply_result);

select @multiply_result;

接著,利用資料庫配置資訊建立連線及遊標物件

import pymysql

PY_MYSQL_CONN_DICT = {

“host”: ‘127。0。0。1’,

“port”: 3306,

“user”: ‘root’,

“passwd”: ‘root’,

“db”: ‘test_db’

}

# 資料庫連線

db_conn = pymysql。connect(**PY_MYSQL_CONN_DICT)

# 遊標

db_cursor = db_conn。cursor(cursor=

pymysql。cursors。DictCursor)

最後,使用函式「 callproc 」呼叫儲存過程名稱及所有引數,獲取返回值

在執行完儲存過程後,需要透過遊標物件的「 execute 」函式獲取出參及入參

db_cursor。callproc(‘num_multi’, args=(3, 6, -1))

# 獲取入參及出參

db_cursor。execute(‘SELECT @_num_multi_0, @_num_multi_1, @_num_multi_2’)

# 出參值

output_result = db_cursor。fetchone()[‘@_num_multi_2’]

# 出參值

print(output_result)

需要注意的是,如果儲存過程涉及到更新、新增等操作,需要顯式呼叫 commit() 函式,才會真正提交到資料庫中

4。 最後

上面僅僅羅列出儲存過程的常見語法,包含 case 條件分支處理、repeat 和 loop 迴圈可以自己去擴充套件學習