新聞中心
如何在mysql中使用存儲過程?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
存儲過程包含了一系列可執(zhí)行的sql語句,存儲過程存放于MySQL中,通過調用它的名字可以執(zhí)行其內部的一堆sql
存儲過程的優(yōu)點
#1. 用于替代程序寫的SQL語句,實現(xiàn)程序與sql解耦
#2. 可以通過直接修改存儲過程的方式修改業(yè)務邏輯(或bug),而不用重啟服務器
#3. 執(zhí)行速度快,存儲過程經過編譯之后會比單獨一條一條執(zhí)行要快
#4. 減少網絡傳輸,尤其是在高并發(fā)情況下這點優(yōu)勢大,存儲過程直接就在數(shù)據(jù)庫服務器上跑,所有的數(shù)據(jù)訪問都在服務器內部進行,不需要傳輸數(shù)據(jù)到其它終端。
存儲過程的缺點
1.SQL本身是一種結構化查詢語言,加上了一些控制(賦值、循環(huán)和異常處理等),但不是OO的,本質上還是過程化的,面對復雜的業(yè)務邏輯,過程化的處理會很吃力。這一點算致命傷,即只能應用在邏輯簡單的業(yè)務上。
2.不便于調試?;旧蠜]有較好的調試器,很多時候是用print來調試,但用這種方法調試長達數(shù)百行的存儲過程簡直是噩夢。好吧,這一點不算啥,C#/java一樣能寫出噩夢般的代碼。
3.沒辦法應用緩存。雖然有全局臨時表之類的方法可以做緩存,但同樣加重了數(shù)據(jù)庫的負擔。如果緩存并發(fā)嚴重,經常要加鎖,那效率實在堪憂。
4.無法適應數(shù)據(jù)庫的切割(水平或垂直切割)。數(shù)據(jù)庫切割之后,存儲過程并不清楚數(shù)據(jù)存儲在哪個數(shù)據(jù)庫中。
無參的存儲過程
delimiter // create procedure p1() BEGIN select * from blog; INSERT into blog(name,sub_time) values("xxx",now()); END // delimiter ;
#在mysql中調用 call p1()
#在python中基于pymysql調用 cursor.callproc('p1') print(cursor.fetchall())
有參的存儲過程
對于存儲過程,可以接收參數(shù),其參數(shù)有三類:
#in 僅用于傳入參數(shù)用
#out 僅用于返回值用
#inout 既可以傳入又可以當作返回值
帶in的存儲過程
mysql> select * from emp; +----+----------+-----+--------+ | id | name | age | dep_id | +----+----------+-----+--------+ | 1 | zhangsan | 18 | 1 | | 2 | lisi | 19 | 1 | | 3 | egon | 20 | 2 | | 5 | alex | 18 | 2 | +----+----------+-----+--------+ 4 rows in set (0.30 sec) mysql> delimiter // mysql> create procedure p2(in n1 int, in n2 int) -> begin -> select * from emp where id >n1 and idend // Query OK, 0 rows affected (0.28 sec) mysql> delimiter ; mysql> call p2(1,3) -> ; +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 2 | lisi | 19 | 1 | +----+------+-----+--------+ 1 row in set (0.07 sec) Query OK, 0 rows affected (0.07 sec)
#在python中基于pymysql調用 cursor.callproc('p2',(1,3)) print(cursor.fetchall())
帶有out
mysql> delimiter // mysql> create procedure p3( in n1 int, out res int) -> begin -> select * from emp where id >n1; -> set res=1; -> end // Query OK, 0 rows affected (0.28 sec) mysql> delimiter ; mysql> set @res=0; Query OK, 0 rows affected (0.00 sec) mysql> call p3(3,@res); +----+------+-----+--------+ | id | name | age | dep_id | +----+------+-----+--------+ | 5 | alex | 18 | 2 | +----+------+-----+--------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec) mysql> select @res; +------+ | @res | +------+ | 1 | +------+ 1 row in set (0.00 sec)
#在python中基于pymysql調用 cursor.callproc('p3',(3,0)) #0相當于set @res=0 print(cursor.fetchall()) #查詢select的查詢結果 cursor.execute('select @_p3_0,@_p3_1;') #@p3_0代表第一個參數(shù),@p3_1代表第二個參數(shù),即返回值 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. 聲明如果出現(xiàn)異常則執(zhí)行{ set status = 1; rollback; } 開始事務 -- 由秦兵賬戶減去100 -- 方少偉賬戶加90 -- 張根賬戶加10 commit; 結束 set status = 2; END // delimiter ; #實現(xiàn) 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; #執(zhí)行失敗 insert into blog(name,sub_time) values('yyy',now()); COMMIT; -- SUCCESS set p_return_code = 0; #0代表執(zhí)行成功 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())
存儲過程的執(zhí)行
mysql中執(zhí)行
-- 無參數(shù) call proc_name() -- 有參數(shù),全in call proc_name(1,2) -- 有參數(shù),有in,out,inout set @t1=0; set @t2=3; call proc_name(1,2,@t1,@t2)
pymsql中執(zhí)行
#!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 執(zhí)行存儲過程 cursor.callproc('p1', args=(1, 22, 3, 4)) # 獲取執(zhí)行完存儲的參數(shù) 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中使用存儲過程的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標題:如何在mysql中使用存儲過程-創(chuàng)新互聯(lián)
網頁網址:http://www.dlmjj.cn/article/gidhd.html