DBI.pm fetchrow() issue

S

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 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
 
B

Bob Walton

superfly2 said:
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())

---------------------------^^^^^^^^

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.

{
print OUTPUT "$val\n";
}
....


HTH.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 |
....

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

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP2morGPeouIeTNHoEQIsDQCg7+TIfuXOuc/UPADiPrAg7PCzDMEAn1lY
4dvcSdW95zXL2PpA1QDymz2j
=Aj2U
-----END PGP SIGNATURE-----
 
R

Ryan Shondell

Bob Walton said:
superfly2 wrote: (snip)

---------------------------^^^^^^^^

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top