Discussion:
DBI.pm fetchrow() issue
(too old to reply)
superfly2
2003-09-18 00:22:18 UTC
Permalink
Hi, the while loop I use to print each result of my SQL query seems to stop
when it encounters the FIRST NULL value (although there are other non-NULL
values still left to be printed. How can I avoid this so that I print all
the non-NULL values? Thanks.

My code is as follows:

my $sthv=$dbh->prepare($queryv);
$sthv->execute();

Now, for every output value
while(my $val = $sthv->fetchrow())
{
print OUTPUT "$val\n";
}

It prints:

| Klk7 |
| D10Ucla1 |
| Whsc2h |
| Oaz2 |

Instead of (from a query done directly in SQL):
| Klk7 |
| D10Ucla1 |
| Whsc2h |
| Oaz2 |
| NULL |
| Slc25a2 |
| Slc25a15 |
| Hornerin-pending |
| NULL |
| D6Ucla1e |
| Odcp-pending |
| Whsc1l1 |
| Oaz3 |
| NULL |
| NULL |
| NULL |
| NULL |
| NULL
Steve May
2003-09-18 01:25:27 UTC
Permalink
Post by superfly2
Hi, the while loop I use to print each result of my SQL query seems to stop
when it encounters the FIRST NULL value (although there are other non-NULL
values still left to be printed. How can I avoid this so that I print all
the non-NULL values? Thanks.
Uh.... are you running, or at least testing with warnings on?
Doesn't sound like it...
Post by superfly2
my $sthv=$dbh->prepare($queryv);
$sthv->execute();
Now, for every output value
while(my $val = $sthv->fetchrow())
{
$val or next;
print OUTPUT "$val\n";
Post by superfly2
}
At least at first glance....


s.
Bob Walton
2003-09-18 03:15:52 UTC
Permalink
Post by superfly2
Hi, the while loop I use to print each result of my SQL query seems to stop
when it encounters the FIRST NULL value (although there are other non-NULL
values still left to be printed. How can I avoid this so that I print all
the non-NULL values? Thanks.
my $sthv=$dbh->prepare($queryv);
$sthv->execute();
Now, for every output value
while(my $val = $sthv->fetchrow())
---------------------------^^^^^^^^

Uh, are you sure? DBI does not contain a method called "fetchrow".
Maybe you mean fetchrow_array, but then you should have an array on the
lefthand side. Or maybe fetchrow_arrayref? But then you would need to
refer to the first element as $$val[0], not $val as you did below. So
what exactly is your *real* code? If you use either of the above
methods, you will not have this problem, as either will return a true
value when there is data and a false value when it stops (or an error
occurs), as in:

while(my @array=$sthv->fetchrow_array){...

or

while(my $array_ref=$sthv->fetchrow_arrayref){...

as is clearly stated in the DBI documentation.
Post by superfly2
{
print OUTPUT "$val\n";
}
...


HTH.
--
Bob Walton
Ryan Shondell
2003-09-18 14:40:13 UTC
Permalink
(snip)
Post by Bob Walton
Post by superfly2
while(my $val = $sthv->fetchrow())
---------------------------^^^^^^^^
Uh, are you sure? DBI does not contain a method called
"fetchrow".
Although not in the documentation, a brief search in DBI.pm shows this
line in the list of methods...

fetchrow => undef, # old alias for fetchrow_array

(This is DBI version 1.2)

Ryan
--
perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
$;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
=shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
+shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
Eric J. Roode
2003-09-18 12:44:44 UTC
Permalink
Post by superfly2
Hi, the while loop I use to print each result of my SQL query seems to
stop when it encounters the FIRST NULL value (although there are other
non-NULL values still left to be printed. How can I avoid this so that
I print all the non-NULL values? Thanks.
my $sthv=$dbh->prepare($queryv);
$sthv->execute();
Now, for every output value
while(my $val = $sthv->fetchrow())
{
print OUTPUT "$val\n";
}
| Klk7 |
| D10Ucla1 |
| Whsc2h |
| Oaz2 |
| Klk7 |
| D10Ucla1 |
| Whsc2h |
| Oaz2 |
| NULL |
| Slc25a2 |
| Slc25a15 |
...

First of all, please copy/paste your real code, instead of re-typing it.
DBI does not have a fetchrow() method for statement handles, afaik.
Also, your print statement doesn't have any "|" symbols in it, so that's
not the output from that code fragment. Who knows what other differences
there are between your real code and what you posted?

Second, NULL is generally represented as undef. So your while condition:

while (my $val = $sthv->fetchrow())

(if such a method existed) would return undef for a NULL column value,
which is false, which would terminate your loop. Consider using
fetchall_arrayref to grab them all at once, then iterating over the
returned array refernce. Or, the following should work (warning:
untested):

while (my ($val) = $sthv->fetchrow_array)

- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
Loading...