Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://983802]
[Eily]: there's a rivalry between the two cities, so defining one in terms of the other might not be well received
[Eily]: erix well I did click on the walking icon
LanX giggles!
[LanX]: Eily I'm duing this constantly with people from rival cities ...MUCHO fun!
[Eily]: The best example of that is the TGV, they couldn't decide who would get it so it's in the middle of nowhere halfway between the two
[LanX]: Eily: that rings a bell
[erix]: interesting difference, I suppose they use different data/routes (and shorter seems better, no?)
[LanX]: Montabaur station
[erix]: (I used http://afstandmete )
LanX The stations of Limburg Süd and Montabaur, which are approximately 20 km apart, ...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2017-12-13 15:24 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (369 votes). Check out past polls.