Guest User

Untitled

a guest
Jan 1st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. # Connect to the database.
  2. my $dbh = DBI->connect(
  3. "DBI:mysql:database=DB;host=>IP",
  4. "hostname", 'password',
  5. {'RaiseError' => 1,'AutoCommit'=> 0}
  6. );
  7.  
  8. open (FILE, 'file.log');
  9. while (<FILE>) {
  10.  
  11. ($word1, $word2, $word3, $word4, $word5, $word6, $word7, $word8, $word9, $word10, $word11, $word12, $word13, $word14) = split(" ");
  12.  
  13. $word13 =~ s/[^d.]//g;
  14. if ($word2 eq "Feb") {
  15. $word2 = "02"
  16. }
  17.  
  18. print "'$word5-$word2-$word3 $word4', $word11, $word13 n";
  19.  
  20. eval {
  21. #on peut utiliser insert mais il y aura des doublons et ici on est dans une table unique
  22. my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES('$word5-$word2-$word3 $word4', $word11, $word13);");
  23.  
  24. #print $sth->rows . " rows found.n";
  25. #$sth->finish;
  26.  
  27. # do inserts, updates, deletes, queries here
  28. #$sth->execute() or die "execution failed: $dbh->errstr()";
  29. $sth->execute() or die "execution failed: $dbh->errstr()";
  30. $dbh->commit();
  31.  
  32. };
  33.  
  34. ### If something went wrong...
  35.  
  36. }
  37. }
  38.  
  39. $dbh->disconnect();
  40.  
  41. use strict;
  42. use warnings;
  43.  
  44. # Connect to the database.
  45. my $dbh = DBI->connect(
  46. "DBI:mysql:database=DB;host=>IP",
  47. "hostname", 'password',
  48. {'RaiseError' => 1,'AutoCommit'=> 0}
  49. );
  50.  
  51. # prepare the insert statement
  52. my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES(?, ?, ?)");
  53. $sth->{RaiseError} = 0;
  54.  
  55. open (my $file, 'file.log') or die "could not open : $!";
  56. while (<$file>) {
  57. my @words = split / /;
  58. $words[12] =~ s/[^d.]//g;
  59. if ($words[1] eq "Feb") {
  60. $words[1] = "02" ;
  61. }
  62.  
  63. # print "'$words[4]-$words[1]-$words[2] $words[3]', $words[10], $words[12] n";
  64. $sth->execute( "$words[4]-$words[1]-$words[2] $words[3]", $words[10], $words[12] );
  65.  
  66. }
  67.  
  68. $dbh->commit;
  69. $dbh->disconnect;
  70.  
  71. #!/usr/local/bin/perl
  72.  
  73. use strict;
  74. use warnings;
  75. use DBI;
  76.  
  77. # open the file
  78. open (my $file, 'log.file') or die "could not open : $!";
  79.  
  80. # connect the database
  81. my $dbh = DBI->connect("DBI:mysql:database=DB;host=ip", "hostname", 'password', {'RaiseError' => 1,'AutoCommit'=> 0});
  82.  
  83. # prepare the INSERT statement
  84. my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES(?, ?, ?)");
  85.  
  86. # run bulk INSERTS
  87. my $tuples = $sth->execute_array({
  88. ArrayTupleStatus => my @tuple_status,
  89. ArrayTupleFetch => sub {
  90. my $line = <$file>;
  91. return unless $line;
  92. my @words = split / /;
  93. # ... do anything you like with the array, then ...
  94. return [ "$words[4]-$words[1]-$words[2] $words[3]", $words[10], $words[12] ];
  95. }
  96. });
  97.  
  98. if ($tuples) {
  99. print "Successfully inserted $tuples recordsn";
  100. } else {
  101. # do something usefull with @tuple_status, that contains the detailed results
  102. }
  103.  
  104. $dbh->commit;
  105. $dbh->disconnect;
Add Comment
Please, Sign In to add comment