Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Connect to the database.
- my $dbh = DBI->connect(
- "DBI:mysql:database=DB;host=>IP",
- "hostname", 'password',
- {'RaiseError' => 1,'AutoCommit'=> 0}
- );
- open (FILE, 'file.log');
- while (<FILE>) {
- ($word1, $word2, $word3, $word4, $word5, $word6, $word7, $word8, $word9, $word10, $word11, $word12, $word13, $word14) = split(" ");
- $word13 =~ s/[^d.]//g;
- if ($word2 eq "Feb") {
- $word2 = "02"
- }
- print "'$word5-$word2-$word3 $word4', $word11, $word13 n";
- eval {
- #on peut utiliser insert mais il y aura des doublons et ici on est dans une table unique
- my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES('$word5-$word2-$word3 $word4', $word11, $word13);");
- #print $sth->rows . " rows found.n";
- #$sth->finish;
- # do inserts, updates, deletes, queries here
- #$sth->execute() or die "execution failed: $dbh->errstr()";
- $sth->execute() or die "execution failed: $dbh->errstr()";
- $dbh->commit();
- };
- ### If something went wrong...
- }
- }
- $dbh->disconnect();
- use strict;
- use warnings;
- # Connect to the database.
- my $dbh = DBI->connect(
- "DBI:mysql:database=DB;host=>IP",
- "hostname", 'password',
- {'RaiseError' => 1,'AutoCommit'=> 0}
- );
- # prepare the insert statement
- my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES(?, ?, ?)");
- $sth->{RaiseError} = 0;
- open (my $file, 'file.log') or die "could not open : $!";
- while (<$file>) {
- my @words = split / /;
- $words[12] =~ s/[^d.]//g;
- if ($words[1] eq "Feb") {
- $words[1] = "02" ;
- }
- # print "'$words[4]-$words[1]-$words[2] $words[3]', $words[10], $words[12] n";
- $sth->execute( "$words[4]-$words[1]-$words[2] $words[3]", $words[10], $words[12] );
- }
- $dbh->commit;
- $dbh->disconnect;
- #!/usr/local/bin/perl
- use strict;
- use warnings;
- use DBI;
- # open the file
- open (my $file, 'log.file') or die "could not open : $!";
- # connect the database
- my $dbh = DBI->connect("DBI:mysql:database=DB;host=ip", "hostname", 'password', {'RaiseError' => 1,'AutoCommit'=> 0});
- # prepare the INSERT statement
- my $sth = $dbh->prepare("INSERT INTO `test_query` (time, cstep, time_in_seconde) VALUES(?, ?, ?)");
- # run bulk INSERTS
- my $tuples = $sth->execute_array({
- ArrayTupleStatus => my @tuple_status,
- ArrayTupleFetch => sub {
- my $line = <$file>;
- return unless $line;
- my @words = split / /;
- # ... do anything you like with the array, then ...
- return [ "$words[4]-$words[1]-$words[2] $words[3]", $words[10], $words[12] ];
- }
- });
- if ($tuples) {
- print "Successfully inserted $tuples recordsn";
- } else {
- # do something usefull with @tuple_status, that contains the detailed results
- }
- $dbh->commit;
- $dbh->disconnect;
Add Comment
Please, Sign In to add comment