濮阳杆衣贸易有限公司

主頁 > 知識庫 > MySQL游標(biāo)概念與用法詳解

MySQL游標(biāo)概念與用法詳解

熱門標(biāo)簽:400電話申請廠家現(xiàn)貨 中國地圖標(biāo)注公司 天津公司外呼系統(tǒng)軟件 百度地圖標(biāo)注要什么軟件 福建外呼電銷機(jī)器人加盟 昌德訊外呼系統(tǒng) 自己做地圖標(biāo)注需要些什么 電話機(jī)器人的價(jià)格多少錢一個(gè)月 徐涇鎮(zhèn)騰訊地圖標(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)先手動(dòng)進(jìn)行fetch操作獲取到一行數(shù)據(jù),然后再通過循環(huán),在循環(huán)里先做處理內(nèi)容,后進(jìn)行fetch操作。這樣如果在手動(dòng)獲取數(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ì)有所幫助。

您可能感興趣的文章:
  • MySQL 游標(biāo)的定義與使用方式
  • Mysql 存儲過程中使用游標(biāo)循環(huán)讀取臨時(shí)表
  • mysql聲明游標(biāo)的方法
  • 詳解Mysql 游標(biāo)的用法及其作用
  • mysql游標(biāo)的原理與用法實(shí)例分析
  • 帶你徹底搞懂python操作mysql數(shù)據(jù)庫(cursor游標(biāo)講解)
  • mysql存儲過程之游標(biāo)(DECLARE)原理與用法詳解
  • mysql的存儲過程、游標(biāo) 、事務(wù)實(shí)例詳解
  • Mysql存儲過程中游標(biāo)的用法實(shí)例
  • Mysql存儲過程循環(huán)內(nèi)嵌套使用游標(biāo)示例代碼
  • MySQL存儲過程中游標(biāo)循環(huán)的跳出和繼續(xù)操作示例
  • MySQL 游標(biāo)的作用與使用相關(guān)

標(biāo)簽:駐馬店 北京 黔西 梅河口 鄂爾多斯 荊門 昌都 陜西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL游標(biāo)概念與用法詳解》,本文關(guān)鍵詞  MySQL,游標(biāo),概念,與,用法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL游標(biāo)概念與用法詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL游標(biāo)概念與用法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    青海省| 石河子市| 玛曲县| 新郑市| 寿光市| 台州市| 阜平县| 元朗区| 荔波县| 蒲城县| 盐津县| 成武县| 二手房| 云安县| 四子王旗| 南召县| 浦江县| 台山市| 长兴县| 广安市| 兰西县| 娄底市| 保靖县| 荥经县| 普格县| 工布江达县| 清丰县| 凌云县| 宝丰县| 白山市| 蛟河市| 循化| 聂荣县| 鹤庆县| 莲花县| 稷山县| 阿图什市| 南安市| 邵阳市| 崇文区| 通辽市|