douzaoqu4447 2014-11-24 23:52
浏览 53
已采纳

检查行是否已被解析并插入到mysql数据库中

I am parsing a whole javascript file (min_day.js) into arrays and then creating INSERT Statements; then executing them with a mysqli_multi_query to get the data into my database

this min_day.js file is updating irregularly (in most cases every 5 min) during the day but it still holds the data from the same day: that means always a new line with data is added on top of the old data

so i am going to configure a cronjob that runs every min to do the parsing and inserting mentioned in the first paragraph but:

now the problem is: how can i only parse and insert the data that has not already been parsed and inserted into the database? how can i check if the data has already been parsed and inserted?

i want to avoid having twice the same data in my database. i guess the solution could be something with using a timestamp... but i'm a beginner and don't know how

// min_day.js file
m[mi++]="24.11.14 08:30:00|196;124;132;55;540;601;45|194;112;123;53;538;606;45|457;350;120;149;570;541;45|452;336;114;146;566;544;46|428;323;107;145;569;541;45|409;325;114;137;572;541;45|38;50;11;407;10|0;0;0;251;14|0;0;0;253;14|11;8;73;0.0;3|16;9;74;0.0;5|13;7;74;0.0;3|16;8;75;0.0;4|18;8;74;0.0;6|0;0;0;310"

m[mi++]="24.11.14 08:25:00|151;106;104;39;539;594;45|147;90;102;37;538;589;45|355;273;96;111;564;540;44|351;259;94;109;566;534;46|348;280;87;110;563;539;45|331;269;97;103;569;536;45|28;38;8;377;10|0;0;0;228;14|0;0;0;239;14|10;8;73;0.0;2|14;8;74;0.0;4|11;7;74;0.0;2|13;8;75;0.0;3|15;8;74;0.0;4|0;0;0;303"

m[mi++]="24.11.14 08:20:00|110;84;85;27;535;586;45|113;74;82;26;533;586;44|283;229;81;83;564;539;44|282;213;76;81;564;536;45|283;223;73;82;566;539;44|266;232;81;76;566;540;45|19;30;0;394;10|0;0;0;230;14|0;0;0;228;14|6;8;73;0.0;1|9;8;74;0.0;3|6;7;74;0.0;1|9;7;75;0.0;2|11;7;74;0.0;3|0;0;0;279"

m[mi++]="24.11.14 08:15:00|94;82;80;18;535;594;44|93;70;76;17;534;599;44|264;215;76;59;558;534;43|262;198;74;58;560;534;45|260;211;66;58;564;537;44|248;208;76;54;560;534;44|17;28;0;394;10|0;0;0;229;14|0;0;0;228;14|6;8;73;0.0;1|9;8;74;0.0;2|5;7;74;0.0;1|8;7;75;0.0;1|10;7;74;0.0;2|0;0;0;281"

m[mi++]="24.11.14 08:10:00|45;47;64;0;556;573;44|62;50;58;9;543;587;43|190;156;59;38;561;528;43|188;148;57;36;557;523;44|189;158;51;37;561;526;44|179;163;61;34;560;521;44|1;11;0;454;10|0;0;0;216;14|0;0;0;213;14|4;8;73;0.0;0|7;8;74;0.0;1|3;7;74;0.0;0|4;7;75;0.0;0|7;7;74;0.0;1|0;0;0;197"

m[mi++]="24.11.14 08:05:00|49;54;55;5;519;551;43|52;44;51;4;517;553;43|151;139;49;22;499;477;42|147;126;47;20;496;466;44|132;120;39;21;530;501;43|130;136;50;19;493;466;43|0;0;0;353;7|0;0;0;206;14|0;0;0;203;14|3;8;73;0.0;0|6;8;74;0.0;0|2;7;74;0.0;0|3;7;75;0.0;0|5;7;74;0.0;0|0;0;0;121"

m[mi++]="24.11.14 08:00:00|36;50;47;1;520;524;43|36;42;48;0;521;531;42|118;112;44;10;470;446;42|116;106;40;9;473;448;44|114;114;37;10;477;452;43|104;120;44;9;471;447;43|0;0;0;0;0|0;0;0;196;14|0;0;0;192;14|3;8;73;0.0;0|6;8;74;0.0;0|1;7;74;0.0;0|2;7;75;0.0;0|5;7;74;0.0;0|0;0;0;118"

m[mi++]="24.11.14 07:56:00|0;12;15;0;641;641;42|0;1;16;0;641;640;42|75;82;33;5;470;446;42|72;83;33;3;473;448;43|74;83;29;5;477;452;42|66;91;36;4;471;447;43|0;0;0;0;0|0;0;0;0;0|0;0;0;0;0|1;8;73;0.0;0|4;8;74;0.0;0|1;7;74;0.0;0|1;7;75;0.0;0|3;7;74;0.0;0|0;0;0;115"

m[mi++]="24.11.14 07:50:00|0;0;0;0;0;0;0|0;0;0;0;0;0;0|0;19;9;0;586;567;41|0;10;3;0;584;564;42|0;10;0;0;590;570;42|0;19;9;0;584;566;42|0;0;0;0;0|0;0;0;0;0|0;0;0;0;0|1;8;73;0.0;0|3;8;74;0.0;0|0;7;74;0.0;0|0;7;75;0.0;0|1;7;74;0.0;0|0;0;0;0"

// php code to parse min_day.js and insert data into db
ini_set('auto_detect_line_endings', true);


$fileArray = file("min_day.js");
$fileArray = array_values(array_filter($fileArray, "trim"));

$arrayElements = count($fileArray) -1;

$SQL = "";

$x = 0; 
while($x <= $arrayElements) 
    {
    $SQL .= "INSERT INTO mydatabase (DatumUhrzeit, Pac_1, Pdc1_1, Pdc2_1, DaySum_1, Udc1_1, Udc2_1, Temp_1, Pac_2, Pdc1_2, Pdc2_2, DaySum_2, Udc1_2, Udc2_2, Temp_2, Pac_3, Pdc1_3, Pdc2_3, DaySum_3, Udc1_3, Udc2_3, Temp_3, Pac_4, Pdc1_4, Pdc2_4, DaySum_4, Udc1_4, Udc2_4, Temp_4, Pac_5, Pdc1_5, Pdc2_5, DaySum_5, Udc1_5, Udc2_5, Temp_5, Pac_6, Pdc1_6, Pdc2_6, DaySum_6, Udc1_6, Udc2_6, Temp_6, Pac_7, Pdc1_7, DaySum_7, Udc1_7, Temp_7, Pac_8, Pdc1_8, DaySum_8, Udc1_8, Temp_8, Pac_9, Pdc1_9, DaySum_9, Udc1_9, Temp_9, SolIrr_10, TmpMod_10, TmpAmb_10, Wind_10, DaySumIrr_10, SolIrr_11, TmpMod_11, TmpAmb_11, Wind_11, DaySumIrr_11, SolIrr_12, TmpMod_12, TmpAmb_12, Wind_12, DaySumIrr_12, SolIrr_13, TmpMod_13, TmpAmb_13, Wind_13, DaySumIrr_13, SolIrr_14, TmpMod_14, TmpAmb_14, Wind_14, DaySumIrr_14, Pac_15, Pdc1_15, DaySum_15, Udc_15) VALUES ";
    $string = $fileArray[$x];
    $string = str_ireplace("|", ";", $string);
    $data=explode(";", substr($string,9, strlen($string)-11));
    $SQL .= "('" . DateTime::createFromFormat('d.m.y H:i:s', $data[0])->format('Y-m-d H:i:s') . "', ";
    for ($i=1; $i<= 85; $i++){
    $data2 = explode(";", $data[$i]);
    $SQL .= "'" . $data[$i] . "', ";
    }
    $SQL .= "'$data[86]'); <br>";
    $x++;
    }
                      
// connecting to db and executing mysqli_multi_query....

</div>
  • 写回答

1条回答 默认 最新

  • dqblm40280 2014-11-25 00:04
    关注

    You can setup proper primary keys and use the insert on duplicate...update syntax:

    --Say A and B together was the primary key.
    
    INSERT INTO table (a,b,c) VALUES (1,2,3)
      ON DUPLICATE KEY UPDATE c=c+1;
    
    -- Row will be inserted with A=1, B=2, C=3
    
    INSERT INTO table (a,b,c) VALUES (1,2,3)
      ON DUPLICATE KEY UPDATE c=c+1;
    
    -- Row where A=1, B=2 will be updated to C=4
    

    Second insert would not give an error but would rather result in the value of C being updated on the already existing row.

    In your case, you can update the timestamp:

    INSERT INTO table (a,b,ts) VALUES (1,2,current_timestamp)
      ON DUPLICATE KEY UPDATE ts=current_timestamp;
    

    To create a primary key on a table that already exists:

    alter table tablename add primary key(field1, field2);
    

    To create a primary key on a new table:

    create table newtable ( x int, y int, z varchar(50), primary key(x,y) );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效