知識點:Mysql 基本用法之存儲過程
阿新 • • 發佈:2018-05-28
rollback word tar for from iam tin ack --
存儲過程
一、 介紹
存儲過程包含了一系列可執行的sql語句,存儲過程存放於MySQL中,通過調用它的名字可以執行其內部的一堆sql
使用存儲過程的優點:
- 用於替代程序寫的SQL語句,實現程序與sql解耦
- 基於網絡傳輸,傳別名的數據量小,而直接傳sql數據量大
使用存儲過程的缺點:
- 程序員擴展功能不方便
補充:程序與數據庫結合使用的三種方式
- 方式一:
MySQL:存儲過程
程序:調用存儲過程
- 方式二:
MySQL:
程序:純SQL語句
- 方式三:
MySQL:
程序:類和對象,即ORM(本質還是純SQL語句)
二、 創建簡單存儲過程(無參)
無參的例子
delimiter //#定義sql的結束語句為// create procedure p1() BEGIN select * from blog; INSERT into blog(name,sub_time) values("xxx",now()); END // delimiter ; #定義sql的結束語句為; #在mysql中調用 call p1() #在python中基於pymysql調用 cursor.callproc(‘p1‘) print(cursor.fetchall())
三、 創建存儲過程(有參)
對於存儲過程,可以接收參數,其參數有三類:
in 僅用於傳入參數用
out 僅用於返回值用
inout 既可以傳入又可以當作返回值
in 的運用實例:
delimiter // create procedure p2( in n1 int, in n2 int ) BEGIN select * from blog where id > n1; END // delimiter ; #在mysql中調用 call p2(3,2) #在python中基於pymysql調用 cursor.callproc(‘p2‘,(3,2)) print(cursor.fetchall())
out 的運用實例:
delimiter // create procedure p3( in n1 int, out res int ) BEGIN select * from blog where id > n1; set res = 1; END // delimiter ; #在mysql中調用 set @res=0; #0代表假(執行失敗),1代表真(執行成功) call p3(3,@res); select @res; #在python中基於pymysql調用 cursor.callproc(‘p3‘,(3,0)) #0相當於set @res=0 print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute(‘select @_p3_0,@_p3_1;‘) #@_p3_0代表第一個參數,@_p3_1代表第二個參數,即返回值 print(cursor.fetchall())
inout 的運用實例:
delimiter // create procedure p4( inout n1 int ) BEGIN select * from blog where id > n1; set n1 = 1; END // delimiter ; #在mysql中調用 set @x=3; call p4(@x); select @x; #在python中基於pymysql調用 cursor.callproc(‘p4‘,(3,)) print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute(‘select @_p4_0;‘) print(cursor.fetchall())
事務和存儲過程的實例:
#介紹 delimiter // create procedure p4( out status int ) BEGIN 1. 聲明如果出現異常則執行{ set status = 1; rollback; } 開始事務 -- 大木木賬戶減去100 -- 二木木賬戶加90 -- 三木木賬戶加10 commit; 結束 set status = 2; END // delimiter ; #實現 delimiter // create PROCEDURE p5( OUT p_return_code tinyint ) BEGIN DECLARE exit handler for sqlexception BEGIN -- ERROR set p_return_code = 1; rollback; END; DECLARE exit handler for sqlwarning BEGIN -- WARNING set p_return_code = 2; rollback; END; START TRANSACTION; DELETE from tb1; #執行失敗 insert into blog(name,sub_time) values(‘yyy‘,now()); COMMIT; -- SUCCESS set p_return_code = 0; #0代表執行成功 END // delimiter ; #在mysql中調用存儲過程 set @res=123; call p5(@res); select @res; #在python中基於pymysql調用存儲過程 cursor.callproc(‘p5‘,(123,)) print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute(‘select @_p5_0;‘) print(cursor.fetchall())
四、 執行存儲過程
在mysql 中執行存儲過程:
-- 無參數 call proc_name() -- 有參數,全in call proc_name(1,2) -- 有參數,有in,out,inout set @t1=0; set @t2=3; call proc_name(1,2,@t1,@t2) 執行存儲過程
在python 中基於pymysql 執行存儲過程:
import pymysql conn = pymysql.connect(host=‘127.0.0.1‘, port=3306, user=‘root‘, password=‘123‘, db=‘t1‘) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 執行存儲過程 cursor.callproc(‘p1‘, args=(1, 22, 3, 4)) # 獲取執行完存儲的參數 cursor.execute("select @_p1_0,@_p1_1,@_p1_2,@_p1_3") result = cursor.fetchall() conn.commit() cursor.close() conn.close() print(result)
五、 刪除存儲過程
刪除語法:
drop procedure proc_name;
附:Mysql 基本用法
一、【Mysql 基本用法之視圖】
二、【Mysql 基本用法之觸發器】
三、【Mysql 基本用法之事務】
四、【Mysql 基本用法之存儲過程】
五、【Mysql 基本用法之函數】
六、【Mysql 基本用法之流程控制】
知識點:Mysql 基本用法之存儲過程