selectcol_arrayref: secret weapon

Июнь 27th, 2012 | Posted by self in Pensieve

Despite what the name suggests and what quick references say, selectcol_arrayref can be used with more than one column, and not only the first one, which makes it a pretty handy tool, as in some cases it’s much less cumbersome than usual selectall_arrayref and alike.

Normal use (to get values from the first column):

my $tables = $dbh->selectcol_arrayref("SHOW TABLES IN test");
foreach my $table (@$tables) { ... }

This is self-explanatory, we want to do something with every table in a schema.
Now, I also want to know if a table is a base table or a view, and do something different based on that.

my $tbl_ref = $dbh->selectcol_arrayref("SHOW FULL TABLES IN test", { Columns=>[1,2] } );
my %tables = @$tbl_ref;
foreach my $t (keys %tables) {
if ($tables{$t} eq 'VIEW') { ... }
else { ... }
}

Why is it easier than selectall_arrayref?
Just because I don’t need to bother about the second-level array reference, everything is already in the plain sight. Simple is good.

What can I say, reading documentation can be useful…
http://search.cpan.org/dist/DBI/DBI.pm#selectcol_arrayref

You can follow any responses to this entry through the RSS 2.0 Both comments and pings are currently closed.