Обсуждение: Perl error: fetchrow_hashref failed
Hi, I wonder if I might tap the vast knowledge here againg. I have a perl script that is looping through a statement handle like this. $statement = "select * from sometable"; $sth=$dbh->prepare($statement); $sth->execute(); # here is the part I have a question about. while (%hash_ref = %{$sth->fetchrow_hashref}) { while (($k,$v) = each (%hash_ref)) { print ("\n$k == $v"); } } The problem is that when I have exhausted the statement handle in the first while statement I get this error. DBD::Pg::st fetchrow_array failed: no statement executing It's not fatal, but it is filling my error log. Is there a way I can suppers this? Does it hurt anything for me to be loop through the statement like this? Thanks in advance Jeff Seese
----- Original Message ----- From: "Jeffrey" <seesej@uswest.net> To: <pgsql-general@postgresql.org> Sent: Wednesday, April 12, 2000 10:05 AM Subject: [GENERAL] Perl error: fetchrow_hashref failed > Hi, I wonder if I might tap the vast knowledge here againg. > > I have a perl script that is looping through a statement handle like this. > > $statement = "select * from sometable"; > $sth=$dbh->prepare($statement); > $sth->execute(); > > # here is the part I have a question about. > while (%hash_ref = %{$sth->fetchrow_hashref}) { > > while (($k,$v) = each (%hash_ref)) { > print ("\n$k == $v"); } > } I don't think your first loop will work because the %{$b} where $b is undef will not produce an undef - I think. See if this works? It ought to be more efficient anyway, because you only need one hash in my version, not two like your routine (that created by the fetchrow_hashref call and the hash %hashref itself. while (defined $hash_ref=$sth->fetchrow_hashref) { while (($k,$v)=each %$hashref) { print ("\n$k==$v"); } }
Your script works fine, but I still get the same error. I can duplicate this same error with a non-hash ref like this. $sth=$dbh->prepare("select param1,param2 from sometable"); $sth->execute; while (($param1,$param2) = $sth->fetchrow_array) { ...... do something with them . } My concern is, does the error fetchrow_hashref or _array cause any problems with the server, postmaster, postgres, etc. I am about to turn a site on with lots of traffic and I don't want to crash because of these errors. Thank you js > From: "Moray McConnachie" <moray.mcconnachie@computing-services.oxford.ac.uk> > Date: Wed, 12 Apr 2000 12:45:42 +0100 > To: "Jeffrey" <seesej@uswest.net>, " " <pgsql-general@postgresql.org> > Subject: Re: [GENERAL] Perl error: fetchrow_hashref failed > > > ----- Original Message ----- > From: "Jeffrey" <seesej@uswest.net> > To: <pgsql-general@postgresql.org> > Sent: Wednesday, April 12, 2000 10:05 AM > Subject: [GENERAL] Perl error: fetchrow_hashref failed > > >> Hi, I wonder if I might tap the vast knowledge here againg. >> >> I have a perl script that is looping through a statement handle like this. >> >> $statement = "select * from sometable"; >> $sth=$dbh->prepare($statement); >> $sth->execute(); >> >> # here is the part I have a question about. >> while (%hash_ref = %{$sth->fetchrow_hashref}) { >> >> while (($k,$v) = each (%hash_ref)) { >> print ("\n$k == $v"); } >> } > > I don't think your first loop will work because the %{$b} where $b is undef > will not produce an undef - I think. > > See if this works? It ought to be more efficient anyway, because you only > need one hash in my version, not two like your routine (that created by the > fetchrow_hashref call and the hash %hashref itself. > > while (defined $hash_ref=$sth->fetchrow_hashref) { > while (($k,$v)=each %$hashref) { > print ("\n$k==$v"); > } > } > > > >
All I can say is I get no errors of this kind in my code, which runs $sth=$dbh->prepare("select param1,param2 from sometable"); $sth->execute; while (@resultset = $sth->fetchrow_array) { ...... do something with them . } $sth->finish; I don't think calling $sth-finish should make the difference, but maybe if you have already used $sth once there is a problem? Try it anyway :-> ---------------------------------------------------------------- Moray.McConnachie@computing-services.oxford.ac.uk ----- Original Message ----- From: "Jeffrey" <seesej@uswest.net> To: "Moray McConnachie" <moray.mcconnachie@computing-services.oxford.ac.uk>; <pgsql-general@postgresql.org> Sent: Wednesday, April 12, 2000 12:57 PM Subject: Re: [GENERAL] Perl error: fetchrow_hashref failed > Your script works fine, but I still get the same error. > > I can duplicate this same error with a non-hash ref like this. > > $sth=$dbh->prepare("select param1,param2 from sometable"); > $sth->execute; > > while (($param1,$param2) = $sth->fetchrow_array) { > ...... do something with them . > } > > My concern is, does the error fetchrow_hashref or _array cause any problems > with the server, postmaster, postgres, etc. I am about to turn a site on > with lots of traffic and I don't want to crash because of these errors. > > Thank you > js > > > From: "Moray McConnachie" <moray.mcconnachie@computing-services.oxford.ac.uk> > > Date: Wed, 12 Apr 2000 12:45:42 +0100 > > To: "Jeffrey" <seesej@uswest.net>, " " <pgsql-general@postgresql.org> > > Subject: Re: [GENERAL] Perl error: fetchrow_hashref failed > > > > > > ----- Original Message ----- > > From: "Jeffrey" <seesej@uswest.net> > > To: <pgsql-general@postgresql.org> > > Sent: Wednesday, April 12, 2000 10:05 AM > > Subject: [GENERAL] Perl error: fetchrow_hashref failed > > > > > >> Hi, I wonder if I might tap the vast knowledge here againg. > >> > >> I have a perl script that is looping through a statement handle like th is. > >> > >> $statement = "select * from sometable"; > >> $sth=$dbh->prepare($statement); > >> $sth->execute(); > >> > >> # here is the part I have a question about. > >> while (%hash_ref = %{$sth->fetchrow_hashref}) { > >> > >> while (($k,$v) = each (%hash_ref)) { > >> print ("\n$k == $v"); } > >> } > > > > I don't think your first loop will work because the %{$b} where $b is undef > > will not produce an undef - I think. > > > > See if this works? It ought to be more efficient anyway, because you only > > need one hash in my version, not two like your routine (that created by the > > fetchrow_hashref call and the hash %hashref itself. > > > > while (defined $hash_ref=$sth->fetchrow_hashref) { > > while (($k,$v)=each %$hashref) { > > print ("\n$k==$v"); > > } > > } > > > > > > > > > > >
I think the cleanest way to handle this is: while ($Rhash = $sth->fetchrow_hashref) { do something with %$Rhash or just refer to $Rhash->{fieldname} } The array-based version below, if I remember correctly, can fail in certain cases, including if both params are undefined. The code above succeeds because the assignment is always either a hashref or undefined, never an array partially consisting of undefined values. BTW, the DBI docs suggest that the *fastest* way to do it is with $sth->fetchrow_arrayref. Cheers, Andy Perrin > All I can say is I get no errors of this kind in my code, which runs > > $sth=$dbh->prepare("select param1,param2 from sometable"); > $sth->execute; > > while (@resultset = $sth->fetchrow_array) { > ...... do something with them . > } > $sth->finish; > > > I don't think calling $sth-finish should make the difference, but maybe if > you have already used $sth once there is a problem? > Try it anyway :-> > ---------------------------------------------------------------- > Moray.McConnachie@computing-services.oxford.ac.uk > ----- Original Message ----- > From: "Jeffrey" <seesej@uswest.net> > To: "Moray McConnachie" <moray.mcconnachie@computing-services.oxford.ac.uk>; > <pgsql-general@postgresql.org> > Sent: Wednesday, April 12, 2000 12:57 PM > Subject: Re: [GENERAL] Perl error: fetchrow_hashref failed > > > > Your script works fine, but I still get the same error. > > > > I can duplicate this same error with a non-hash ref like this. > > > > $sth=$dbh->prepare("select param1,param2 from sometable"); > > $sth->execute; > > > > while (($param1,$param2) = $sth->fetchrow_array) { > > ...... do something with them . > > } > > > > My concern is, does the error fetchrow_hashref or _array cause any > problems > > with the server, postmaster, postgres, etc. I am about to turn a site on > > with lots of traffic and I don't want to crash because of these errors. > > > > Thank you > > js > > > > > From: "Moray McConnachie" > <moray.mcconnachie@computing-services.oxford.ac.uk> > > > Date: Wed, 12 Apr 2000 12:45:42 +0100 > > > To: "Jeffrey" <seesej@uswest.net>, " " <pgsql-general@postgresql.org> > > > Subject: Re: [GENERAL] Perl error: fetchrow_hashref failed > > > > > > > > > ----- Original Message ----- > > > From: "Jeffrey" <seesej@uswest.net> > > > To: <pgsql-general@postgresql.org> > > > Sent: Wednesday, April 12, 2000 10:05 AM > > > Subject: [GENERAL] Perl error: fetchrow_hashref failed > > > > > > > > >> Hi, I wonder if I might tap the vast knowledge here againg. > > >> > > >> I have a perl script that is looping through a statement handle like th > is. > > >> > > >> $statement = "select * from sometable"; > > >> $sth=$dbh->prepare($statement); > > >> $sth->execute(); > > >> > > >> # here is the part I have a question about. > > >> while (%hash_ref = %{$sth->fetchrow_hashref}) { > > >> > > >> while (($k,$v) = each (%hash_ref)) { > > >> print ("\n$k == $v"); } > > >> } > > > > > > I don't think your first loop will work because the %{$b} where $b is > undef > > > will not produce an undef - I think. > > > > > > See if this works? It ought to be more efficient anyway, because you > only > > > need one hash in my version, not two like your routine (that created by > the > > > fetchrow_hashref call and the hash %hashref itself. > > > > > > while (defined $hash_ref=$sth->fetchrow_hashref) { > > > while (($k,$v)=each %$hashref) { > > > print ("\n$k==$v"); > > > } > > > } > > > > > > > > > > > > > > > > > > >