atemon has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks, you may think this program a crazy one, but I think it worth to discuss.
I was trying to fetch data from two different tables into separate hashes. I always use 'strict'. I use DBI's function bind_columns with the fancy syntax. The code is
I get the errormy $data = {}; my $SQL = "SELECT * FROM my_table ORDER BY order"; my $sth = $dbh->prepare( $SQL ); $sth->execute() or die DBI->errstr; my %row; $sth->bind_columns( \( @row{ @{ $sth->{ NAME_uc } } } )); $i = 1; while ( $sth->fetch() ){ push @names, $row{ 'NAME' }; delete $row{ 'NAME' }; #this line causes problem a +t 'bind_columns' down there $data->{"data_$i"} = { %row }; $i++; } $sth->finish(); my $second_data = {}; $SQL2 = "SELECT * FROM my_second_table ORDER BY order"; my $sth2 = $dbh->prepare( $SQL2 ); $sth2->execute(); my %row2 = (); $sth2->bind_columns( \( @row2 { @{ $sth2->{ NAME_lc } } } )); # Er +ror due to delete $row{ 'NAME' }; at top $i=1; while ( $sth2->fetch() ){ $second_data->{"data_$i"} = { %row2 }; $i++; } $sth2->finish();
The line causing error is"Can't use an undefined value as an ARRAY reference at /myprogram.pl l +ine 23."
When I comment line delete $row{ 'NAME' }; everything works fine.$sth2->bind_columns( \( @row2 { @{ $sth2->{ NAME_lc } } } ));
I wonder why it throws this error since first set of variables has NO connection with the second set, except that they use same DB handle.
Other Observations I have made are
This also failed :(while ( $sth->fetch() ){ %record=%row; push @names, $record{ 'NAME' }; delete $record{ 'NAME' }; $data->{"data_$i"} = { %record }; $i++; }
This also made NO differencewhile ( $sth->fetch() ){ %record=(%row); push @names, $record{ 'NAME' }; delete $record{ 'NAME' }; $data->{"data_$i"} = \%record; $i++; }
Thanks in advance for the help.
--VC
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI::bind_colums throws error when element of binding hash is deleteed
by andreas1234567 (Vicar) on Oct 16, 2007 at 08:27 UTC | |
|
Re: DBI::bind_colums throws error when element of binding hash is deleteed
by perrin (Chancellor) on Oct 16, 2007 at 12:25 UTC | |
|
Re: DBI::bind_colums throws error when element of binding hash is deleteed
by tcf03 (Deacon) on Oct 16, 2007 at 12:26 UTC | |
|
Re: DBI::bind_colums throws error when element of binding hash is deleteed
by runrig (Abbot) on Oct 16, 2007 at 16:53 UTC | |
|
Re: DBI::bind_colums throws error when element of binding hash is deleteed
by atemon (Chaplain) on Oct 17, 2007 at 04:38 UTC |