in reply to Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI

Just a minor comment:
SELECT *
I would replace that with
SELECT column1, column2, ..
to ensure that you get the columns in the order you expect.

Continue down the same road with the fetch:

while ( my @arr = $sth->fetchrow_array() )
I would replace that with
while ( my $href = $sth->fetchrow_hashref() ) { my $col1 = $href->{column1}; my $col2 = $href->{column2}; ... }
This way I'm certain that my variables correspond to the values by name, not just by their position in the array.
--
print map{chr}unpack(q{A3}x24,q{074117115116032097110111116104101114032080101114108032104097099107101114})
  • Comment on Re: Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Extra iteration while fetching 'fetchrow_array' in DBI and standerd way of using DBI
by runrig (Abbot) on Jun 15, 2007 at 17:38 UTC
    SELECT *

    I sometimes use 'select *' but it is usually combined with something like:

    $sth->execute(); my @columns = $sth->{NAME_uc}; my %row; $sth->bind_columns( \@row{@columns} ); while ( $sth->fetch() ) { ... }
    But then you have to be watch out for columns that get renamed or deleted that you explicitly use in the while loop, etc. (and all selected fields must have an explicit name, e.g., you need an alias for calculated fields).
      runrig:

      I like the trick to bind the columns up, but I still wouldn't use a select * if only because someone might WTF-ify the database and you'll be reading quite a bit more data than you need. Also, future maintainers won't know which columns are significant without significant spelunking through the code.

      --roboticus