DBI.pm fetchrow() issue

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default DBI.pm fetchrow() issue

    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


    superfly2 Guest

  2. Similar Questions and Discussions

    1. Am I the only one still having this issue
      I realize IE put a fix in place in April 2008 for swf flash to no longer require click to activate. Am I the only one who still has this issue. I am...
    2. XML Issue
      I recently created a flash animation that calls the dynamic text from a XML file. The movie works when I use flash player, however when it is...
    3. FRS issue
      I have started having replication problems and know the reason I just have no choice in the matter. Management has insisted on running ZA...
    4. Fetchrow Uninitialized Values
      Hello all. I have a script that connects to a PostgreSQL database, executes a single SELECT, and dumps the results to the screen. Although it...
    5. IIS 5.1 and ASP.net issue
      I'd suggest posting to a DotNet group...this one is for ASP (classic asp, not ASPX) :} --...
  3. #2

    Default Re: DBI.pm fetchrow() issue

    superfly2 wrote:
    > 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...
    >
    > My code is as follows:
    >
    > my $sthv=$dbh->prepare($queryv);
    > $sthv->execute();
    >
    > Now, for every output value
    > while(my $val = $sthv->fetchrow())
    > {
    >
    $val or next;
    print OUTPUT "$val\n";
    > }
    >

    At least at first glance....


    s.

    Steve May Guest

  4. #3

    Default Re: DBI.pm fetchrow() issue

    superfly2 wrote:
    > 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.
    --
    Bob Walton

    Bob Walton Guest

  5. #4

    Default Re: DBI.pm fetchrow() issue

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

    "superfly2" <darius_fatakia@yahoo.com> wrote in
    news:bkatrs$215$1@news.Stanford.EDU:
    > 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-----
    Eric J. Roode Guest

  6. #5

    Default Re: DBI.pm fetchrow() issue

    Bob Walton <bwalton@rochester.rr.com> writes:
    > superfly2 wrote:
    (snip)
    > > 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;;'
    Ryan Shondell Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139