Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: DBD::ORacle: Not able to insert into Database

by mje (Curate)
on Jul 26, 2012 at 08:25 UTC ( #983802=note: print w/ replies, xml ) Need Help??


in reply to Re^2: DBD::ORacle: Not able to insert into Database
in thread DBD::ORacle: Not able to insert into Database

  1. The "chomp issue" was that you expected chomp to return the chomped string but it instead returns the number of characters chomped.
  2. I don't believe you needed to remove the primary key. I think you had some other issue but that you changed more than one thing at once. If the column values are unique or you are going to select via that column you want an index or unique index. As you started with problems attempting to read from a file with <> I suspect you somehow where reading from stdin and hence it looked like your script was hung. That is why I suggested pressing ctrl/d.
  3. It was suggested you inserted the values as parameters and that is still the best way to do it. As you have it you are having to build a SQL string each time when in fact before your while loop you could simply do one prepare with placeholders and then only call execute inside the while loop. It would also avoid all those quote calls.


Comment on Re^3: DBD::ORacle: Not able to insert into Database
Re^4: DBD::ORacle: Not able to insert into Database
by slayedbylucifer (Scribe) on Jul 28, 2012 at 05:26 UTC
    mje, I will be working on your suggestion and will modify the script accordingly. Will let you know how it goes. Thanks for your time.
Re^4: DBD::ORacle: Not able to insert into Database
by slayedbylucifer (Scribe) on Jul 30, 2012 at 08:40 UTC

    mje, here is my code after following your suggestions.

    sub upload { my ($csv_file, $table, $col1, $col2, $col3) = ($_[0], $_[1], $ +_[2], $_[3], $_[4]); my $csv = Text::CSV->new ( { binary => 1 } ) or die "Cannot us +e CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", $csv_file or die " $!"; <$fh>; ### this is to remove the column headers. my $dbh = DBI->connect ("dbi:Oracle:host=<hostname>t;sid=SID;p +ort=1526", 'username', 'password', { RaiseError => 1, AutoCommit => 1, Tr +aceLevel => 0 }) or die "Cannot create Database Handle: + $DBI::errstr()"; my $sth = $dbh->prepare ("INSERT INTO $table ($col1, $col2, $c +ol3) VALUES (?,?,?)"); while ( my $row = $csv->getline ($fh)) { $sth->execute ($row->[0], $row->[1], $row->[2]); } $sth->finish(); $dbh->disconnect(); close $fh; }

    Thank you very much for guiding me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-12-28 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls