RE: (Fwd) DBI's method for reading [row x,field y]
- From: Philip.Garrett@xxxxxxxxxxx (Philip Garrett)
- Date: Tue, 27 Feb 2007 17:18:43 -0500
-----Original Message-----
From: Bob Hunter [mailto:catdogbeloved@xxxxxxxxx]
Sent: Tue 2/27/2007 5:07 PM
To: Garrett, Philip (MAN-Corporate); dbi-users@xxxxxxxx
Cc: catdogbeloved@xxxxxxxxx
Subject: RE: (Fwd) DBI's method for reading [row x,field y]
The same example, using Pg...
# use Pg;
# my $dbh = Pg::connectdb("dbname=<dbname>");
# my $sth = $dbh->exec("<SQL STATEMENT>");
# for (my $i = 0; $i < $sth->ntuples; $i++) {
# for (my $j = 0; $j < $sth->nfields; $j++) {
# print "Value at ($i,$j):
$sth->getvalue($i,$j)\n";
# }}
It is more concise, and more intuitive.
It is a pity that DBI is so cumbersome.
Yes, DBI is faster and independent from specific
databases, but Pg is far more elegant. Too bad one
cannot have both worlds.
Beauty is in the eye of the beholder. Your code example is not very
"Perlish" -- it is accessing database records at a low level like
a C-style multidimensional array, rather than as named fields in records.
I, for one, think this is more intuitive than your example, since it
treats the data by name rather than by some computer-assigned
numeric index:
my $dbh = DBI->connect;
my $sth = $dbh->prepare("SELECT * FROM BOOKS");
$sth->execute;
while (my $book = $sth->fetchrow_hashref) {
print "$book->{ISBN}: $book->{TITLE}\n";
}
or
my $sth = $dbh->prepare("SELECT TITLE, ISBN FROM BOOKS");
$sth->execute;
while (my ($title,$isbn) = $sth->fetchrow) {
print "$isbn: $title\n";
}
Regards,
Philip
- References:
- RE: (Fwd) DBI's method for reading [row x,field y]
- From: Bob Hunter
- RE: (Fwd) DBI's method for reading [row x,field y]
- Prev by Date: Re: (Fwd) DBI's method for reading [row x,field y]
- Next by Date: Re: (Fwd) DBI's method for reading [row x,field y]
- Previous by thread: RE: (Fwd) DBI's method for reading [row x,field y]
- Next by thread: Re: (Fwd) DBI's method for reading [row x,field y]
- Index(es):