So, here's a usage example...# This is my means of a sort of 'bind params by column name'. sub bind_result_columns { my $sth = shift; my $RESULT_MAP = shift; my $bound_fields = {}; while (my ($node_field, $db_col) = each %$RESULT_MAP) { $db_col = lc $db_col; if (defined $sth->{NAME_lc_hash}{$db_col}) { $sth->bind_col( $sth->{NAME_lc_hash}{$db_col} + 1, \$bound_fields->{$node_field} ); } else { die "DB column $db_col not returned from your SQL query!\n +"; } } return $bound_fields; }
my $QUERY = "select READ_COMMUNITY,IP_ADDRESS,DEVICE_GROUP,MANAGED fro +m SNMP_NODES"; # Do your connect, prepare, execute, etc... my $dbh = DBI->connect($DB_DSN, $DB_USER, $DB_PASS, $DB_OPTIONS) or die "Couldn't connect to DB: " . $DBI::errstr; my $sth = $dbh->prepare($QUERY) or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute(); ### Map the node definition fields to the SQL result columns ### my $RESULT_MAP = { 'read_community' => 'READ_COMMUNITY', 'address' => 'IP_ADDRESS', 'on_off' => 'MANAGED', 'group' => 'DEVICE_GROUP', }; my $bound_fields = bind_result_columns($sth, $RESULT_MAP); while ($sth->fetch) { # Do stuff with the data now... print Dumper $bound_fields; # DEBUG }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI bind output vars by column name
by rob_au (Abbot) on Jul 10, 2007 at 04:35 UTC | |
|
Re: DBI bind output vars by column name
by injunjoel (Priest) on Jul 13, 2007 at 17:28 UTC | |
by Hercynium (Hermit) on Jul 14, 2007 at 00:07 UTC |