http://www.perlmonks.org?node_id=1177487


in reply to not able to insert data in database using perl DBI

The error message is:

Undefined subroutine &main::execute called at ...

This originates from this line:

$sth = execute(...)

That's not how you should call the ->execute method of a statement handle. Use $sth->... as you do everywhere else.

Replies are listed 'Best First'.
Re^2: not able to insert data in database using perl DBI
by Anonymous Monk on Dec 08, 2016 at 14:29 UTC

    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

      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] );