Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: not able to insert data in database using perl DBI

by Anonymous Monk
on Dec 08, 2016 at 14:29 UTC ( #1177496=note: print w/replies, xml ) Need Help??


in reply to Re: not able to insert data in database using perl DBI
in thread not able to insert data in database using perl DBI

Hi sachin,

I did some minor reformatting of your code and used array splices instead of your rather long assignments

You already heared of array splices?

I changed what Corion suggested and also moved the prepare statement out of the loop, because this is the sense of a prepare statement

I also declared a new srray, because naming something like $column_1 (or $first_column and so on) always looks like an array structure.

But maybe this naming has to do with your contrieved example.

use strict; use warnings;; my $dir = "tt"; my $date = "dd"; my $brnchid = "kk"; my $path = join "/", $dir, $date; my $dbfile = "xxx.xx.xx.xx"; my $dsn = "dbi:Oracle:host=$dbfile"; my $user = "ma"; my $password = "mama"; my $dbh = DBI->connect( 'dbi:Oracle:host=xx.xx.xx.xx;sid=orcl;port=1521;SERVER=POOLED', $user, $password, { PrintError => 1, RaiseError => 1, AutoCommit => 1, FetchHashKeyName => 'NAME_lc', TraceLevel => 4, } ) or die "Cannot connect to Database: $DBI::errstr\n"; chdir($path); open( my $fh, '< nwsa.txt' ) or die "Could not open file $!"; my $sql = "INSERT INTO nwsa (code_id,name_code,inr_col,out_col,brnch_i +d) VALUES (?,?,?,?,?)"; print "$sql\n"; my $sth = $dbh->prepare($sql); while ( my $current_row = <$fh> ) { if ( $current_row =~ /^[AL]/ ) { my @sachin = split //, $current_row; my @column; # a fresh array for each row $column[1] = join( "", @sachin[0,6] ); $column[2] = join( "", @sachin[7,77] ); $column[3] = join( "", @sachin[85,101] ); my @newdigit = split /[,]+/, $column[3]; my $string = join( '', map {"$_"} @newdigit ); my $Receipt_Amount = ( $string * 100 ) / 100; print "$Receipt_Amount\n"; $column[4] = join( "", @sachin[106,126] ); my @newdigit1 = split /[,]+/, $column[4]; $string = join( '', map {"$_"} @newdigit1 ); my $Receipt_Amount1 = ( $string * 100 ) / 100; print "$Receipt_Amount1\n"; print "$column[1],", "$column[2],", "$Receipt_Amount,", "$Rece +ipt_Amount1,", "$date,", "$brnchid \n"; my $rc = $sth->execute( $column[1], $column[2], $Receipt_Amoun +t, $Receipt_Amount1, $brnchid ); #$sth->finish(); # counterproductive esp. when m +oving prepare statement out of while loop } } $dbh->disconnect(); close($fh);

Hope this will work as intended, I have no Oracle host to work with.

Cheers Bedani

Replies are listed 'Best First'.
Re^3: not able to insert data in database using perl DBI
by fishmonger (Chaplain) on Dec 08, 2016 at 15:36 UTC

    Your array slices need a slight adjustment. Instead of the comma, you need to use the range operator. Otherwise you'd only be getting 2 of the fields in each statement instead of the full required range.

    $column[1] = join( "", @sachin[0 .. 6] ); $column[2] = join( "", @sachin[7 .. 77] ); $column[3] = join( "", @sachin[85 .. 101] );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-05-19 16:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?