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; } }
In reply to Perl csv insert with special character by homer4all
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |