爱程序网

MySQL中游标使用以及读取文本数据

来源: 阅读:

前言

之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课。当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试过了。现在学习大数据分析,接触了数据挖掘,才感觉到数据库是不可跨越的坎。直到现在才感觉到《操作系统》、《编译原理》、《计算机组成原理》等等课程的重要性。在浩瀚的知识面前,个人是非常渺小的。掌握了一种思想之后,任何事情都不困难,困难的是你是否真的静下心看一看帮助文档、认真的Google。静心、静气、认真、执着。

游标-cursor

学习了几天MySQL,谈一谈自己对游标的认识。

游标就类似于C++中的指针,用于指向查询结果。比如你查询后的的数据格式如下:

+------------+----------------------+------+------+------+-------------+----------+----------+------+---------+| 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 |+------------+----------------------+------+------+------+-------------+----------+----------+------+---------+

你如果想逐条处理数据,那么必须要用到游标进行循环处理。

加载进来的数据是varchar格式,但是对于第二个属性“get_time”我们需要的格式是“datatime”,需要进行获取属性值并进行循环处理。

使用游标的步骤如下:

1.定义游标 declare 游标名 cursor for select语句

2.定义处理游标结束的变量 declare continue handler for not found  set 变量名= true;

3.打开游标 open 游标名

4.判断是否结束,如果不结束,则处理当前游标指向值;如果结束,则结束循环

5.关闭游标 close 游标名

注:游标一般是在存储过程(procedure)中调用,procedure类似于C++中的函数,里面封装了SQL语句,想要使用时,直接CALL ‘procedure_name’即可。游标(cursor)中若有使用的变量必须在声明cursor前把变量定义完。详细的代码设计如下:

CREATE DEFINER=`root`@`localhost` PROCEDURE `strToDate`()begin  -- 定义一个临时变量用于存储转换后的时间格式declare temp datetime; -- 定义字符串临时变量,存储查询后的每条内容declare str varchar(150); -- 是否结束的标识declare flag int default false; -- 定义游标declare getTimeCursor cursor for select get_time from train; -- 定义结束的标识declare continue handler for not found  set flag = true; -- 打开游标open getTimeCursor; -- 开始循环处理 read_loop:loop  -- 把当前游标内容放到变量中fetch getTimeCursor into str; -- 如果结束标识为TRUE,则结束循环if flag thenleave read_loop;end if; -- 否则循环处理每个属性,调用字符串转换日期函数set temp = (select str_to_date(str,'%c/%e/%Y %l:%i:%s %p')); -- 把转换结果存储到新的表中insert into time_test values(temp); -- 结束循环end loop; -- 关闭游标close getTimeCursor; -- 查询结果select * from time_test limit 10;end

其中,str_to_date()函数的功能是把string类型的数据转成date类型。查询后的结果为:

+---------------------+| 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,所有字符串都转换成了标准的时间格式。

MySQL load data控制

其实上面的问题完全可以利用另外一种方法完成,那就是在装载数据的时候进行格式控制。具体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 localinfile 'f:\dataset\beijing\crawleddata.txt'into table traindatafields 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;

加载进数据库后,具体数据格式如下:

+----+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+| 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,标准的数据格式。

 

原创内容,转载请注明出处。http://www.cnblogs.com/chuantingSDU/p/4243990.html

联系方式:chuanting.zhang@gmail.com

相关文章列表:
关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助