Output results from a DBI select query in one line
mjg
created: 2006-04-11 11:47:42
I'm finding myself using this idiom a lot for a current project. Adjust to taste for your own applications.
# given a DBI handle $dbh, a database table "foo" with a column "bar"

print join "\n", map join("\t", @$_), @{$dbh->selectall_arrayref( q{
   select bar from foo
} )};
Re: Output results from a DBI select query in one line
created: 2006-04-11 12:12:15
Why not just:
$_->execute(4, 5), $_->dump_results for $dbh->prepare(q{
  SELECT one, two FROM three WHERE four = ? AND five = ?
});

-- [http://www.stonehenge.com/merlyn/|Randal L. Schwartz, Perl hacker]
Be sure to read [id://205373|my standard disclaimer] if this is a reply.

Re^2: Output results from a DBI select query in one line
created: 2006-04-11 14:09:15
Or, a couple of characters shorter than that (if we don't count loading Dumper):
print Dumper $dbh->selectall_arrayref("SELECT one, two FROM three WHERE four=? AND five=?",{},4,5);
Re^3: Output results from a DBI select query in one line
mjg
created: 2006-04-11 14:30:42
Either merlyn's or jZed's examples are fine for diagnostics. The original, however:
  • won't truncate values as dump_results does
  • provides more human-readable output than Data::Dumper's structures
But feel free to use whatever works for you. Mine was just more appropriate for my project.

perlmonks.org content © perlmonks.org and jZed, merlyn, mjg

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03