MySQL游標(biāo)概念與用法詳解
本文實(shí)例講述了MySQL游標(biāo)概念與用法。分享給大家供大家參考,具體如下:
1、游標(biāo)的概念(Cursor)
一條sql,對應(yīng)N條資源,取出資源的接口,就是游標(biāo),沿著游標(biāo),可以一次取出1行。如果開發(fā)過安卓的同學(xué)應(yīng)該知道有一個(gè)Api是Cursor,也是讀取SQLite數(shù)據(jù)庫用的,和這個(gè)有點(diǎn)類似。
2、使用游標(biāo)的步驟
(1)聲明
使用declare進(jìn)行聲明
declare 游標(biāo)名 cursor for select_statement
(2)打開游標(biāo)
使用open進(jìn)行打開
open 游標(biāo)名
(3)從游標(biāo)中取值
使用fetch進(jìn)行取值
fetch 游標(biāo)名 into var1,var2[,...] --將取到的一行賦值給多個(gè)變量
(4)關(guān)閉游標(biāo)
使用close關(guān)閉游標(biāo)
close 游標(biāo)名
3、創(chuàng)建一個(gè)簡單的游標(biāo)
需求:從商品表中讀取第一行數(shù)據(jù)
商品表(goods)數(shù)據(jù):
注意:我這里已經(jīng)將MySQL的結(jié)束標(biāo)識符改為 $,如果要知道怎么設(shè)置為$,請參考前面一篇文章:MySQL觸發(fā)器。
定義:
create procedure p12() begin /*定義三個(gè)變量用于存放商品id,商品名稱,商品庫存量*/ declare row_gid int ; declare row_name varchar(20); declare row_num int; declare getgoods cursor for select gid,name,num from goods; --定義游標(biāo) open getgoods; --打開游標(biāo) fetch getgoods into row_gid,row_name,row_num;--從游標(biāo)中取值 select row_name,row_num; --顯示操作 close getgoods; --關(guān)閉游標(biāo) end$
輸出結(jié)果:
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$
輸出:
注意:當(dāng)游標(biāo)讀到末尾,如果繼續(xù)進(jìn)行取值操作會發(fā)生報(bào)錯(cuò)
5、游標(biāo)循環(huán)表中的所有數(shù)據(jù)
(1)使用計(jì)數(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$
輸出結(jié)果:
(2)使用越界標(biāo)志來控制循環(huán)
在mysql cursor中,可以聲明declare continue handler
來操作1個(gè)越界標(biāo)志
語法:
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$
輸出結(jié)果:
注意:這里發(fā)生了錯(cuò)誤,這里輸出了4行數(shù)據(jù),而表中只有3行數(shù)據(jù),而且還爆出了警告,后面會說怎么結(jié)果這個(gè)問題。
程序執(zhí)行邏輯:
循環(huán)游標(biāo)->fetch第三條數(shù)據(jù)->顯示->fetch第四條數(shù)據(jù)->沒有數(shù)據(jù)->設(shè)置have=0操作->執(zhí)行continue Handler->程序不退出,執(zhí)行顯示操作->還是顯示第三條數(shù)據(jù)
6、continue和exit的區(qū)別
continue:若沒有數(shù)據(jù)返回,程序繼續(xù),并將變量IS_FOUND設(shè)為0,這種情況是出現(xiàn)在select XX into XXX from tablename的時(shí)候發(fā)生的。
exit:若沒有數(shù)據(jù)返回,退出程序,并將變量IS_FOUND設(shè)為0,這種情況是出現(xiàn)在select XX into XXX from tablename的時(shí)候發(fā)生的。
使用exit來替換continue:
使用exit就不會出現(xiàn)上面的那種情況了,程序執(zhí)行邏輯:
循環(huán)游標(biāo)->fetch到第三條數(shù)據(jù)->顯示->第四次fetch操作->沒有數(shù)據(jù)->設(shè)置 have=0操作->程序直接退出exit
所以就沒有顯示出第四條數(shù)據(jù)。
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$
輸出結(jié)果:
7、正確的游標(biāo)循環(huán)
在一些特殊的情況中,我們可以讀到的數(shù)據(jù)為空,或者壓根sql語句就有錯(cuò)誤,我們不能避免出現(xiàn)這種情況,所以我們要正確的使用游標(biāo)循環(huán)操作。
首先應(yīng)該創(chuàng)建游標(biāo),然后打開游標(biāo)后,應(yīng)先手動進(jìn)行fetch操作獲取到一行數(shù)據(jù),然后再通過循環(huán),在循環(huán)里先做處理內(nèi)容,后進(jìn)行fetch操作。這樣如果在手動獲取數(shù)據(jù)的期間就沒有獲得到數(shù)據(jù)的話,就會執(zhí)行have = 0,如果是repeat循環(huán),然后進(jìn)入repeat循環(huán),先輸出null數(shù)據(jù),最后又進(jìn)行獲取,這樣運(yùn)行到until時(shí)就會退出循環(huán);如果是while循環(huán),壓根就不進(jì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$
輸出結(jié)果:
(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$
輸出結(jié)果:
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
相關(guān)文章
CentOS7.3下mysql 8.0.13安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了CentOS7.3下mysql 8.0.13安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11mysql+mybatis實(shí)現(xiàn)存儲過程+事務(wù)?+?多并發(fā)流水號獲取
這篇文章主要介紹了mysql+mybatis+存儲過程+事務(wù)?+?多并發(fā)流水號獲取,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12MySQL數(shù)據(jù)庫使用規(guī)范總結(jié)
本篇文章給大家詳細(xì)分類總結(jié)了數(shù)據(jù)庫相關(guān)規(guī)范,幫助大家發(fā)揮出數(shù)據(jù)庫的性能,感興趣的朋友可以了解下2020-08-08Mac os 解決無法使用localhost連接mysql問題
今天在mac上搭建好了php的環(huán)境,把先前在window、linux下運(yùn)行良好的程序放在mac上,居然出現(xiàn)訪問不了數(shù)據(jù)庫,數(shù)據(jù)庫連接的host用的是localhost,可以確認(rèn)數(shù)據(jù)庫配置是正確的,下面特為大家分享下2014-05-05解決net start mysql--服務(wù)無法啟動 服務(wù)沒有報(bào)告任何錯(cuò)誤問題
這篇文章主要介紹了解決net start mysql--服務(wù)無法啟動 服務(wù)沒有報(bào)告任何錯(cuò)誤問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12使用SQL實(shí)現(xiàn)小計(jì),合計(jì)以及排序
本篇文章是對SQL實(shí)現(xiàn)小計(jì),合計(jì)以及排序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06