日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

MySQL中游標(biāo)使用以及讀取文本數(shù)據(jù)

系統(tǒng) 2207 0
原文: MySQL中游標(biāo)使用以及讀取文本數(shù)據(jù)

前言

之前一直沒(méi)有接觸數(shù)據(jù)庫(kù)的學(xué)習(xí),只是本科時(shí)候修了一本數(shù)據(jù)庫(kù)基本知識(shí)的課。當(dāng)時(shí)只對(duì)C++感興趣,天真的認(rèn)為其它的課都沒(méi)有用,數(shù)據(jù)庫(kù)也是半懂不懂,胡亂就考試過(guò)了。現(xiàn)在學(xué)習(xí)大數(shù)據(jù)分析,接觸了數(shù)據(jù)挖掘,才感覺(jué)到數(shù)據(jù)庫(kù)是不可跨越的坎。直到現(xiàn)在才感覺(jué)到《操作系統(tǒng)》、《編譯原理》、《計(jì)算機(jī)組成原理》等等課程的重要性。在浩瀚的知識(shí)面前,個(gè)人是非常渺小的。掌握了一種思想之后,任何事情都不困難,困難的是你是否真的靜下心看一看幫助文檔、認(rèn)真的Google。靜心、靜氣、認(rèn)真、執(zhí)著。

游標(biāo)-cursor

學(xué)習(xí)了幾天MySQL,談一談自己對(duì)游標(biāo)的認(rèn)識(shí)。

游標(biāo)就類似于C++中的指針,用于指向查詢結(jié)果。比如你查詢后的的數(shù)據(jù)格式如下:

      
        +
      
      
        --
      
      
        ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+
      
      
        |
      
       station_id 
      
        |
      
       get_time             
      
        |
      
       PM25 
      
        |
      
       PM10 
      
        |
      
       NO2  
      
        |
      
       temperature 
      
        |
      
       pressure 
      
        |
      
       humidity 
      
        |
      
       wind 
      
        |
      
       weather 
      
        |
      
      
        +
      
      
        --
      
      
        ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        8
      
      
        /
      
      
        2013
      
      
        9
      
      :
      
        00
      
      :
      
        00
      
       PM  
      
        |
      
      
        149
      
      
        |
      
      
        59
      
      
        |
      
      
        16
      
      
        |
      
      
        -
      
      
        5
      
      
        |
      
      
        1031
      
      
        |
      
      
        46
      
      
        |
      
      
        4
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        8
      
      
        /
      
      
        2013
      
      
        10
      
      :
      
        00
      
      :
      
        00
      
       PM 
      
        |
      
      
        159
      
      
        |
      
      
        65
      
      
        |
      
      
        22
      
      
        |
      
      
        -
      
      
        5
      
      
        |
      
      
        1030
      
      
        |
      
      
        46
      
      
        |
      
      
        1
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        12
      
      :
      
        00
      
      :
      
        00
      
       AM 
      
        |
      
      
        179
      
      
        |
      
      
        73
      
      
        |
      
      
        28
      
      
        |
      
      
        -
      
      
        6
      
      
        |
      
      
        1029
      
      
        |
      
      
        46
      
      
        |
      
      
        4
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        2
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        194
      
      
        |
      
      
        73
      
      
        |
      
      
        29
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1028
      
      
        |
      
      
        49
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        3
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        191
      
      
        |
      
      
        73
      
      
        |
      
      
        27
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1028
      
      
        |
      
      
        50
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        4
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        194
      
      
        |
      
      
        73
      
      
        |
      
      
        25
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1026
      
      
        |
      
      
        53
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        5
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        193
      
      
        |
      
      
        73
      
      
        |
      
      
        23
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1026
      
      
        |
      
      
        54
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        6
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        192
      
      
        |
      
      
        73
      
      
        |
      
      
        21
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1026
      
      
        |
      
      
        52
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        7
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        192
      
      
        |
      
      
        73
      
      
        |
      
      
        23
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1025
      
      
        |
      
      
        54
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        001001
      
      
        |
      
      
        2
      
      
        /
      
      
        9
      
      
        /
      
      
        2013
      
      
        8
      
      :
      
        00
      
      :
      
        00
      
       AM  
      
        |
      
      
        190
      
      
        |
      
      
        73
      
      
        |
      
      
        20
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1025
      
      
        |
      
      
        55
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        +
      
      
        --
      
      
        ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+
      
    

你如果想逐條處理數(shù)據(jù),那么必須要用到游標(biāo)進(jìn)行循環(huán)處理。

加載進(jìn)來(lái)的數(shù)據(jù)是varchar格式,但是對(duì)于第二個(gè)屬性“get_time”我們需要的格式是“datatime”,需要進(jìn)行獲取屬性值并進(jìn)行循環(huán)處理。

使用游標(biāo)的步驟如下:

1.定義游標(biāo) declare 游標(biāo)名 cursor for select語(yǔ)句

2.定義處理游標(biāo)結(jié)束的變量?declare continue handler for not found ?set 變量名= true;

3.打開(kāi)游標(biāo) open 游標(biāo)名

4.判斷是否結(jié)束,如果不結(jié)束,則處理當(dāng)前游標(biāo)指向值;如果結(jié)束,則結(jié)束循環(huán)

5.關(guān)閉游標(biāo) close 游標(biāo)名

注:游標(biāo)一般是在存儲(chǔ)過(guò)程(procedure)中調(diào)用,procedure類似于C++中的函數(shù),里面封裝了SQL語(yǔ)句,想要使用時(shí),直接CALL ‘procedure_name’即可。游標(biāo)(cursor)中若有使用的變量必須在聲明cursor前把變量定義完。詳細(xì)的代碼設(shè)計(jì)如下:

      
        CREATE
      
       DEFINER
      
        =
      
      `root`@`localhost` 
      
        PROCEDURE
      
      
         `strToDate`()


      
      
        begin
      
      
        --
      
      
         定義一個(gè)臨時(shí)變量用于存儲(chǔ)轉(zhuǎn)換后的時(shí)間格式
      
      
        declare
      
      
        temp
      
      
        datetime
      
      
        ;



 
      
      
        --
      
      
         定義字符串臨時(shí)變量,存儲(chǔ)查詢后的每條內(nèi)容
      
      
        declare
      
      
        str
      
      
        varchar
      
      (
      
        150
      
      
        );



 
      
      
        --
      
      
         是否結(jié)束的標(biāo)識(shí)
      
      
        declare
      
       flag 
      
        int
      
      
        default
      
      
         false;



 
      
      
        --
      
      
         定義游標(biāo)
      
      
        declare
      
       getTimeCursor 
      
        cursor
      
      
        for
      
      
        select
      
       get_time 
      
        from
      
      
         train;



 
      
      
        --
      
      
         定義結(jié)束的標(biāo)識(shí)
      
      
        declare
      
      
        continue
      
       handler 
      
        for
      
      
        not
      
       found  
      
        set
      
       flag 
      
        =
      
      
         true;



 
      
      
        --
      
      
         打開(kāi)游標(biāo)
      
      
        open
      
      
         getTimeCursor;



 
      
      
        --
      
      
         開(kāi)始循環(huán)處理
      
      
         read_loop:loop

 

 
      
      
        --
      
      
         把當(dāng)前游標(biāo)內(nèi)容放到變量中
      
      
        fetch
      
       getTimeCursor 
      
        into
      
      
        str
      
      
        ;



 
      
      
        --
      
      
         如果結(jié)束標(biāo)識(shí)為T(mén)RUE,則結(jié)束循環(huán)
      
      
        if
      
       flag 
      
        then
      
      
        

leave read_loop;


      
      
        end
      
      
        if
      
      
        ;



 
      
      
        --
      
      
         否則循環(huán)處理每個(gè)屬性,調(diào)用字符串轉(zhuǎn)換日期函數(shù)
      
      
        set
      
      
        temp
      
      
        =
      
       (
      
        select
      
       str_to_date(
      
        str
      
      ,
      
        '
      
      
        %c/%e/%Y %l:%i:%s %p
      
      
        '
      
      
        ));



 
      
      
        --
      
      
         把轉(zhuǎn)換結(jié)果存儲(chǔ)到新的表中
      
      
        insert
      
      
        into
      
       time_test 
      
        values
      
      (
      
        temp
      
      
        );



 
      
      
        --
      
      
         結(jié)束循環(huán)
      
      
        end
      
      
         loop;



 
      
      
        --
      
      
         關(guān)閉游標(biāo)
      
      
        close
      
      
         getTimeCursor;



 
      
      
        --
      
      
         查詢結(jié)果
      
      
        select
      
      
        *
      
      
        from
      
      
         time_test limit 10;


      
      
        end
      
    

其中,str_to_date()函數(shù)的功能是把string類型的數(shù)據(jù)轉(zhuǎn)成date類型。查詢后的結(jié)果為:

      
        +
      
      
        --
      
      
        -------------------+
      
      
        |
      
       get_time            
      
        |
      
      
        +
      
      
        --
      
      
        -------------------+
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        16
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        08
      
      
        21
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        08
      
      
        22
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        00
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        02
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        03
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        04
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        05
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        06
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        07
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        +
      
      
        --
      
      
        -------------------+
      
    

see,所有字符串都轉(zhuǎn)換成了標(biāo)準(zhǔn)的時(shí)間格式。

MySQL load data控制

其實(shí)上面的問(wèn)題完全可以利用另外一種方法完成,那就是在裝載數(shù)據(jù)的時(shí)候進(jìn)行格式控制。具體SQL代碼如下:

      
        use
      
      
         train;


      
      
        drop
      
      
        table
      
      
         traindata;


      
      
        create
      
      
        table
      
      
        if
      
      
        not
      
      
        exists
      
      
         traindata(

id 
      
      
        int
      
       auto_increment 
      
        primary
      
      
        key
      
      
        ,

station_id 
      
      
        varchar
      
      (
      
        10
      
      
        ),

get_time 
      
      
        datetime
      
      
        ,

PM25 
      
      
        int
      
      
        ,

PM10 
      
      
        int
      
      
        ,

NO2 
      
      
        int
      
      
        ,

temperature 
      
      
        int
      
      
        ,

pressure 
      
      
        int
      
      
        ,

humidity 
      
      
        int
      
      
        ,

wind 
      
      
        double
      
      
        ,

weather 
      
      
        int
      
      
        );




      
      
        load
      
      
         data local

infile 
      
      
        '
      
      
        f:\\dataset\\beijing\\crawleddata.txt
      
      
        '
      
      
        into
      
      
        table
      
      
         traindata

fields terminated 
      
      
        by
      
      
        '
      
      
        ,
      
      
        '
      
      
        

(station_id, 
      
      
        @var_time
      
      
        , PM25, PM10, NO2, temperature, pressure, humidity, wind, weather)


      
      
        set
      
       get_time 
      
        =
      
       str_to_date(
      
        @var_time
      
      , 
      
        '
      
      
        %c/%e/%Y %l:%i:%s %p
      
      
        '
      
      
        );




      
      
        select
      
      
        *
      
      
        from
      
       traindata limit 
      
        10
      
      ;
    

加載進(jìn)數(shù)據(jù)庫(kù)后,具體數(shù)據(jù)格式如下:

      
        +
      
      
        --
      
      
        --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+
      
      
        |
      
       id 
      
        |
      
       station_id 
      
        |
      
       get_time            
      
        |
      
       PM25 
      
        |
      
       PM10 
      
        |
      
       NO2  
      
        |
      
       temperature 
      
        |
      
       pressure 
      
        |
      
       humidity 
      
        |
      
       wind 
      
        |
      
       weather 
      
        |
      
      
        +
      
      
        --
      
      
        --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+
      
      
        |
      
      
        1
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        08
      
      
        21
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        149
      
      
        |
      
      
        59
      
      
        |
      
      
        16
      
      
        |
      
      
        -
      
      
        5
      
      
        |
      
      
        1031
      
      
        |
      
      
        46
      
      
        |
      
      
        4
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        2
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        08
      
      
        22
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        159
      
      
        |
      
      
        65
      
      
        |
      
      
        22
      
      
        |
      
      
        -
      
      
        5
      
      
        |
      
      
        1030
      
      
        |
      
      
        46
      
      
        |
      
      
        1
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        3
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        00
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        179
      
      
        |
      
      
        73
      
      
        |
      
      
        28
      
      
        |
      
      
        -
      
      
        6
      
      
        |
      
      
        1029
      
      
        |
      
      
        46
      
      
        |
      
      
        4
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        4
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        02
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        194
      
      
        |
      
      
        73
      
      
        |
      
      
        29
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1028
      
      
        |
      
      
        49
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        5
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        03
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        191
      
      
        |
      
      
        73
      
      
        |
      
      
        27
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1028
      
      
        |
      
      
        50
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        6
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        04
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        194
      
      
        |
      
      
        73
      
      
        |
      
      
        25
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1026
      
      
        |
      
      
        53
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        7
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        05
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        193
      
      
        |
      
      
        73
      
      
        |
      
      
        23
      
      
        |
      
      
        -
      
      
        7
      
      
        |
      
      
        1026
      
      
        |
      
      
        54
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        8
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        06
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        192
      
      
        |
      
      
        73
      
      
        |
      
      
        21
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1026
      
      
        |
      
      
        52
      
      
        |
      
      
        2
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        9
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        07
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        192
      
      
        |
      
      
        73
      
      
        |
      
      
        23
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1025
      
      
        |
      
      
        54
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        |
      
      
        10
      
      
        |
      
      
        1001
      
      
        |
      
      
        2013
      
      
        -
      
      
        02
      
      
        -
      
      
        09
      
      
        08
      
      :
      
        00
      
      :
      
        00
      
      
        |
      
      
        190
      
      
        |
      
      
        73
      
      
        |
      
      
        20
      
      
        |
      
      
        -
      
      
        8
      
      
        |
      
      
        1025
      
      
        |
      
      
        55
      
      
        |
      
      
        3
      
      
        |
      
      
        1
      
      
        |
      
      
        +
      
      
        --
      
      
        --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+
      
    

see,標(biāo)準(zhǔn)的數(shù)據(jù)格式。

?

原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處。http://www.cnblogs.com/chuantingSDU/p/4243990.html

聯(lián)系方式:chuanting.zhang@gmail.com

MySQL中游標(biāo)使用以及讀取文本數(shù)據(jù)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 丽水市| 安吉县| 通山县| 华亭县| 平昌县| 页游| 咸丰县| 九龙县| 集安市| 桐乡市| 习水县| 朔州市| 肃宁县| 射洪县| 六安市| 任丘市| 廊坊市| 靖宇县| 宁化县| 富川| 黄山市| 登封市| 沙湾县| 陆良县| 新蔡县| 白城市| 阜新市| 绍兴县| 都昌县| 漯河市| 塔城市| 桓仁| 紫云| 江永县| 清河县| 高碑店市| 甘孜| 开封市| 犍为县| 大姚县| 永定县|