Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

slayedbylucifer:

A couple things:

  • Your while loop contains '<' and '>' which aren't necessary: Those tell perl to read from a filehandle. But you've already read your data into the array. Just remove the angle brackets, and that should clear up the first problem.
  • You're not chomping the lines you're reading, so you may be adding newlines to your database that you probably don't want.

Those are the only problems I see. However, you might want to rearrange your code to remove the unnecessary array. You can do so like this (untested):

#!/usr/bin/perl -w use strict; use DBI; open (my $FH, '<', "test.csv") or die "Cannot open test.csv: $! \n"; my $dbh = DBI->connect ("dbi:Oracle:host=<hostname>;sid=<SID>", '<username>', ',password.', { RaiseError => 1, AutoCommit => 1, TraceLev +el => 4 } ) or die "Canot create Databse Handle: $DBI::errstr() \n"; my $sth = $dbh->prepare ("INSERT INTO CLUSTER_MAPPING (name, farm, dmz +) VALUES (?,?,?)"); <$FH>; ### this is to remove the column headers while (<$FH>) { my @row = split /,/; my $col1_value = $row[0]; my $var1 = $row[1]; $var1 =~ s/"//g; $var1 =~ s/^ //g; my $col2_value = $var1; my $col3_value = $row[2]; $sth->execute ($col1_value, $col2_value, $col3_value); } # If this is the end of your program, these lines are unnecessary, as +it'll all be # cleaned up at the end of the program. But if you're continuing to d +o something # else in your program, then by all means leave them in. $sth->finish(); $dbh->disconnect(); close $FH;

I also removed the unnecessary variable $table as I couldn't see the purpose for it.

You can do further simplifications, as well. There's no need to create new variables for the columns, you can just operate on the values in place and use them, like so:

while (<$FH>) { my @row = split /,/; $row[1] =~ s/"//g; $row[1] =~ s/^ //g; $sth->execute (@row); }

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: DBD::ORacle: Not able to insert into Database by roboticus
in thread DBD::ORacle: Not able to insert into Database by slayedbylucifer

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2018-11-14 23:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My code is most likely broken because:
















      Results (178 votes). Check out past polls.

      Notices?