您的位置:首页 > 数据库 > MySQL

利用mySql处理数据——把数据按照指定时间间隔导出

2014-11-25 11:30 288 查看
任务:将近10天的温度数据,9个温度测点,采样间隔为1分钟,共计12万行数据,需要导出每小时的温度数据。

0. 数据格式和数据库设计

数据格式如下:有3列,分别是日期,时间,温度。文件名是测点编号。

 


数据库设计为如下:

create database sensorDB;
use sensorDB;
create table sensor
(
ID smallint ,
dt Date,
tm Time,
temp float,
primary key (ID,dt,tm)
);


并且建立一个视图把dt和tm两个字段合并

create view temperature
as
select ID, cast(CONCAT(dt,' ',tm) as datetime) '时间' , temp from sensor   ;


1.Txt格式文件导入数据库

用C语言写了一个小工具,可以把txt转成sql语句【见后面的源代码】

运行后,输入txt的文件名,要插入的表名,和传感器的编号即可。

程序运行完毕之后,自动生成sql语句如下:



然后直接运行sql语句,数据就导入到数据库中了。

2.按指定间隔查询数据

利用了timestampdiff()来计算时间间隔 ,并利用%来逐个判断

</pre><p></p></blockquote><blockquote style="margin:0 0 0 40px; border:none; padding:0px"><p><span style="font-size:24px"></span></p><pre code_snippet_id="531991" snippet_file_name="blog_20141125_4_1936774" name="code" class="sql">select * from temperature where ID = 9 and  timestampdiff(MINUTE,时间,'2014-04-19 15:00:00')%60 =0;


如下便是间隔1小时的温度数据



附件:txt转sql源代码 C

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

#define BUFFER_LEN 2048
const char sp[] = { ' ', '\t', ';','\r','\n' };

void error(char *msg)
{
printf("ERROR:%s",msg);
fflush(stdin);
getchar();
exit(1);
}
int isSeperator(char  ch)
{

for (int i = 0; i < sizeof(sp) / sizeof(char); i++)
{
if ( ch == sp[i])
{
return 1;
}
}
return 0;
}
int main()
{
printf("=============================\n");
printf("          txt2sql\n");
printf("=============================\n");
printf("data fielname: ");
fflush(stdin);
char filename_in[256], filename_out[256];
scanf("%s",filename_in);
strcpy(filename_out, filename_in);
strcat(filename_out, "_sql.txt");
strcat(filename_in, ".txt");
printf("table name: ");
fflush(stdin);
char table_name[256];
scanf("%s", table_name);

printf("sensor ID: ");
fflush(stdin);
char sensor_ID[256];
scanf("%s", sensor_ID);

FILE *fin = fopen(filename_in, "r");
FILE *fout = fopen(filename_out, "w");
if (fin == NULL || fout == NULL)
{
error("cannot open file");
}
char buffer[BUFFER_LEN];
while (fgets(buffer, BUFFER_LEN, fin) != 0)
{
//除去多余的分隔符
char elem[BUFFER_LEN];
int k = 0;
memset(elem, 0, sizeof(elem));

for (int i = 0; i < strlen(buffer); i++)
{
if (buffer[i] == '\n' || buffer[i] == '\r' || buffer[i]=='\0') buffer[i] = ' ';
}

for (int i = 0; i < strlen(buffer); i++)
{
if (!isSeperator(buffer[i]))
{
elem[k++] = buffer[i];
}
else
{
//如果最后一个是分隔符则跳过
if (i == strlen(buffer) && isSeperator(buffer[i]))
continue;
//如果下一个还是分隔符,则跳过。
if (i != strlen(buffer) - 1 && isSeperator(buffer[i + 1]))
continue;
if (i != strlen(buffer) - 1 && !isSeperator(buffer[i + 1]))
elem[k++] = buffer[i];
}
}
/*printf("\"%s\"",elem);*/
//经过处理的elem只有包含一个分隔符在一起的情况,最后没有空元素

fprintf(fout, "INSERT INTO %s VALUES ('%s',", table_name,sensor_ID);
int left = 0;
for (int i = 0; i < strlen(elem); i++)
{
if (isSeperator(elem[i]))
{

fprintf(fout, "\'");
for (int j = left; j < i; j++)
{
fprintf(fout, "%c", elem[j]);
}
fprintf(fout, "\',");
left = i + 1;
}
}
//the last one
fprintf(fout, "\'");
for (int j = left; j < strlen(elem); j++)
{
fprintf(fout, "%c", elem[j]);
}
fprintf(fout, "\');\n");

}

fclose(fin);
fclose(fout);

printf("Done!");
fflush(stdin);
getchar();
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: