homer4all has asked for the wisdom of the Perl Monks concerning the following question:
With my previous query I have got this far and trying to solve couple of tricky data.
With below code, I'm opening csv file, read each line, insert into table, if any error rollback and move to next csv file.
My file contains 50,000 records but with below script it only enters 500 rows ignoring all other rows.
What I believe is csv data format is messing up with the inserts and somehow it's ignoring rows.1.I think allow_loose_quotes=>1 and allow_whitespace=> needs to be modified to use properly or new parameter?
2.Also how to have n2 value stored as 0.0 and not just 0 in the table OR 0.3 values as 0.3 and not .3?
3.With my current approach it prepare each line and insert, any quick trick to insert 10,000 rows at a time?
Note: this question is also posted by me on another website Stack overflow, inorder to get more expert answer and different ways to solve it.data e.g. (Date, server, n1, n2, command) 20141201 00:00:01, sun_solaris, 30308, 0.0, "sun_gen0_db1 " 20141201 00:00:10, hp_host, 30308, 0.0, "[(jdbc/migration] -/^perl " #!/usr/bin/perl use warnings; use strict; use Text::CSV; use DBD::Oracle; my $exitStatus = 0; # Connect to database and disable autocommit my $dbAgent = OracleAgent->dbLogin(dbName); $dbAgent->connect(); $dbAgent-> setAutoCommit(0); # define logfile my logFile; $logFile = "log.dbinserts" # define csv file directory my $csvLogDir = "Home/log/$csvDow"; # csv Files in array to list all possible match of file opendir(my $dh, $csvLogDir ) || die "can't opendir $csvLogDir : $!"; my @csvFile = grep { /csv.*host1/ && -f "$csvLogDir/$_" } readdir($dh); chomp @csvFile; closedir $dh; foreach my $i (@csvFile) { $logFile (CSV File: $i); } foreach my $file (@csvFile) { $logFile-> ("Working under: $file"); &insertRecords; } $logFile-> ("Exit status") #---------------- sub insertRecords { my $csv; my $fileToInsert = shift; my $row; my $SQL; my $sth; my $dbh; my $rc; open my $fh, "<", $fileToInsert or die "$filetoInsert: $!" $logFile -> ("Working under $file") my $csv = Text::CSV->new ( { binary =>1, auto_diag =>1, allow_loose_quotes =>1, allow_white_space =>1, }); $SQL1 = "Insert into TAB1 (sample_date, server, n1, n2, command) values (?,?,?,?,?)"; $sth = prepare($SQL1); while ($row = $csv->getline ($fh)) { $sth -> execute($row[0], $row[1], $row[2], $row[3], $row[4]); if ($sth1->err) { my $status = "FAIL"; last; } $rc++; } if $status eq 'FAIL' { $dbAgent->rollback(); $rc=0; } else $dbAgent-> commit; close $fh; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl csv insert with special character
by Tux (Canon) on Dec 31, 2014 at 09:13 UTC | |
|
Re: Perl csv insert with special character
by graff (Chancellor) on Dec 31, 2014 at 10:08 UTC | |
|
Re: Perl csv insert with special character
by Anonymous Monk on Dec 30, 2014 at 23:16 UTC | |
by homer4all (Acolyte) on Dec 30, 2014 at 23:24 UTC | |
by Anonymous Monk on Dec 30, 2014 at 23:36 UTC | |
by homer4all (Acolyte) on Dec 30, 2014 at 23:46 UTC | |
by Anonymous Monk on Dec 31, 2014 at 02:57 UTC | |
by homer4all (Acolyte) on Dec 30, 2014 at 23:27 UTC | |
|
Re: Perl csv insert with special character
by Anonymous Monk on Dec 30, 2014 at 23:04 UTC | |
|
Re: Perl csv insert with special character
by GotToBTru (Prior) on Dec 31, 2014 at 14:27 UTC |