MySQL游標概念與用法詳解
本文實例講述了MySQL游標概念與用法。分享給大家供大家參考,具體如下:
1、游標的概念(Cursor)
一條sql,對應N條資源,取出資源的接口,就是游標,沿著游標,可以一次取出1行。如果開發(fā)過安卓的同學應該知道有一個Api是Cursor,也是讀取SQLite數(shù)據庫用的,和這個有點類似。
2、使用游標的步驟
(1)聲明
使用declare進行聲明
declare 游標名 cursor for select_statement
(2)打開游標
使用open進行打開
open 游標名
(3)從游標中取值
使用fetch進行取值
fetch 游標名 into var1,var2[,...] --將取到的一行賦值給多個變量
(4)關閉游標
使用close關閉游標
close 游標名
3、創(chuàng)建一個簡單的游標
需求:從商品表中讀取第一行數(shù)據
商品表(goods)數(shù)據:

注意:我這里已經將MySQL的結束標識符改為 $,如果要知道怎么設置為$,請參考前面一篇文章:MySQL觸發(fā)器。
定義:
create procedure p12() begin /*定義三個變量用于存放商品id,商品名稱,商品庫存量*/ declare row_gid int ; declare row_name varchar(20); declare row_num int; declare getgoods cursor for select gid,name,num from goods; --定義游標 open getgoods; --打開游標 fetch getgoods into row_gid,row_name,row_num;--從游標中取值 select row_name,row_num; --顯示操作 close getgoods; --關閉游標 end$
輸出結果:

4、多次取值操作
create procedure p13() begin declare row_gid int ; declare row_name varchar(20); declare row_num int; declare getgoods cursor for select gid,name,num from goods; open getgoods; fetch getgoods into row_gid,row_name,row_num; select row_name,row_num; fetch getgoods into row_gid,row_name,row_num; select row_name,row_num; fetch getgoods into row_gid,row_name,row_num; select row_name,row_num; fetch getgoods into row_gid,row_name,row_num; select row_name,row_num; close getgoods; end$
輸出:

注意:當游標讀到末尾,如果繼續(xù)進行取值操作會發(fā)生報錯
5、游標循環(huán)表中的所有數(shù)據
(1)使用計數(shù)器來循環(huán)
create procedure p14()
begin
declare cnt int default 0;
declare i int default 0;
declare row_gid int ;
declare row_name varchar(20);
declare row_num int;
declare getgoods cursor for select gid,name,num from goods;
select count(*) into cnt from goods;
open getgoods;
repeat
fetch getgoods into row_gid,row_name,row_num;
select row_name,row_num;
set i:= i+1;
until i >= cnt end repeat;
close getgoods;
end$
輸出結果:

(2)使用越界標志來控制循環(huán)
在mysql cursor中,可以聲明declare continue handler來操作1個越界標志
語法:
declare continue handler for NOT FOUND statement;
使用:
create procedure p15()
begin
declare row_gid int ;
declare row_name varchar(20);
declare row_num int;
declare have int default 1;
declare getgoods cursor for select gid,name,num from goods;
declare continue handler for NOT FOUND set have:= 0;
open getgoods;
repeat
fetch getgoods into row_gid,row_name,row_num;
select row_name,row_num;
until have = 0 end repeat;
close getgoods;
end$
輸出結果:

注意:這里發(fā)生了錯誤,這里輸出了4行數(shù)據,而表中只有3行數(shù)據,而且還爆出了警告,后面會說怎么結果這個問題。
程序執(zhí)行邏輯:
循環(huán)游標->fetch第三條數(shù)據->顯示->fetch第四條數(shù)據->沒有數(shù)據->設置have=0操作->執(zhí)行continue Handler->程序不退出,執(zhí)行顯示操作->還是顯示第三條數(shù)據
6、continue和exit的區(qū)別
continue:若沒有數(shù)據返回,程序繼續(xù),并將變量IS_FOUND設為0,這種情況是出現(xiàn)在select XX into XXX from tablename的時候發(fā)生的。
exit:若沒有數(shù)據返回,退出程序,并將變量IS_FOUND設為0,這種情況是出現(xiàn)在select XX into XXX from tablename的時候發(fā)生的。
使用exit來替換continue:
使用exit就不會出現(xiàn)上面的那種情況了,程序執(zhí)行邏輯:
循環(huán)游標->fetch到第三條數(shù)據->顯示->第四次fetch操作->沒有數(shù)據->設置 have=0操作->程序直接退出exit
所以就沒有顯示出第四條數(shù)據。
create procedure p16()
begin
declare row_gid int ;
declare row_name varchar(20);
declare row_num int;
declare have int default 1;
declare getgoods cursor for select gid,name,num from goods;
declare exit handler for NOT FOUND set have:= 0;
open getgoods;
repeat
fetch getgoods into row_gid,row_name,row_num;
select row_name,row_num;
until have = 0 end repeat;
close getgoods;
end$
輸出結果:

7、正確的游標循環(huán)
在一些特殊的情況中,我們可以讀到的數(shù)據為空,或者壓根sql語句就有錯誤,我們不能避免出現(xiàn)這種情況,所以我們要正確的使用游標循環(huán)操作。
首先應該創(chuàng)建游標,然后打開游標后,應先手動進行fetch操作獲取到一行數(shù)據,然后再通過循環(huán),在循環(huán)里先做處理內容,后進行fetch操作。這樣如果在手動獲取數(shù)據的期間就沒有獲得到數(shù)據的話,就會執(zhí)行have = 0,如果是repeat循環(huán),然后進入repeat循環(huán),先輸出null數(shù)據,最后又進行獲取,這樣運行到until時就會退出循環(huán);如果是while循環(huán),壓根就不進去while循環(huán)里,就不會有任何1行輸出。
(1)repeat循環(huán):
create procedure p17()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare have int default 1;
declare getgoods cursor for select gid,name,num from goods where 0;
declare continue handler for NOT FOUND set have:= 0;
open getgoods;
fetch getgoods into row_gid,row_name,row_num;
repeat
select row_name,row_num;
fetch getgoods into row_gid,row_name,row_num;
until have = 0 end repeat;
close getgoods;
end$
輸出結果:

(2)while循環(huán):
create procedure p18()
begin
declare row_gid int;
declare row_name varchar(20);
declare row_num int;
declare have int default 1;
declare getgoods cursor for select gid,name,num from goods where 0;
declare continue handler for NOT FOUND set have:= 0;
open getgoods;
fetch getgoods into row_gid,row_name,row_num;
while have = 1 do
select row_name,row_num;
fetch getgoods into row_gid,row_name,row_num;
end while;
close getgoods;
end$
輸出結果:

更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數(shù)據庫鎖相關技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據庫計有所幫助。
相關文章
CentOS7.3下mysql 8.0.13安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了CentOS7.3下mysql 8.0.13安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11
mysql+mybatis實現(xiàn)存儲過程+事務?+?多并發(fā)流水號獲取
這篇文章主要介紹了mysql+mybatis+存儲過程+事務?+?多并發(fā)流水號獲取,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
Mac os 解決無法使用localhost連接mysql問題
今天在mac上搭建好了php的環(huán)境,把先前在window、linux下運行良好的程序放在mac上,居然出現(xiàn)訪問不了數(shù)據庫,數(shù)據庫連接的host用的是localhost,可以確認數(shù)據庫配置是正確的,下面特為大家分享下2014-05-05
解決net start mysql--服務無法啟動 服務沒有報告任何錯誤問題
這篇文章主要介紹了解決net start mysql--服務無法啟動 服務沒有報告任何錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

