DB: oracle

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?

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; } }
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.

In reply to Perl csv insert with special character by homer4all

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.