List:General Discussion« Previous MessageNext Message »
From:Michael Stassen Date:March 4 2005 11:41pm
Subject:Re: insert data
In-reply-to:
<C7739D1822C78B45BE6CC05CBB75BC002E3FF1@NEIL.universe.holmescorp.com>
View as plain text  
Right.  First, I think the logic is flawed.  We should successfully 
prepare() or die.  Period.  If the call to prepare() failed ($sth is undef), 
we should not making dying conditional on yet another value.

More to the point, this line is actually the cause of the problem.  (Sorry I 
didn't see it earlier.)  You've run into the precedence rules:

   my $sth = $dbh->prepare( $sql ) or die $dbh->errstr if $dbh->err;

is read as

   (my $sth = $dbh->prepare( $sql ) or die $dbh->errstr) if $dbh->err;

That is, it is equivalent to

   if ($dbh->err)
   {
     $sth = $dbh->prepare( $sql ) or die $dbh->errstr;
   }

Since the connect succeeded, $dbh->err is undef, so we never even call 
prepare!  Hence, $sth is undef when we get to execute, and you get the error 
message.  I expect this is what Joe (John Doe) was trying to tell us earlier.

The simplest solution would be to drop the "if $dbh->err".  That is, change to

   my $sth = $dbh->prepare( $sql ) or die $dbh->errstr;

John's suggestion (below) is better still, as it adds helpful detail to the 
error message when there is one (though I don't see the need to make it a 
separate line of code).

Michael


John Trammell wrote:

> Gerald Preston wrote:
> [snip]
> 
>>my $sth = $dbh->prepare( $sql ) or die $dbh->errstr if $dbh->err;
> 
> [snip]
> 
> Regardless of other problems you may be having, I think you're not
> doing what you want to do here.  How about instead:
> 
> my $sth = $dbh->prepare($sql);
> $sth || die "Error preparing sth from '$sql': ", $dbh->errstr;
> 
Thread
insert dataGerald Preston27 Feb
  • Re: insert dataJohn Doe27 Feb
    • RE: insert dataGerald Preston27 Feb
      • Re: insert dataJohn Doe28 Feb
        • Re: insert dataMichael Stassen28 Feb
        • Re: insert dataMichael Stassen28 Feb
          • RE: insert dataGerald Preston28 Feb
            • RE: insert dataWilliam R. Mussatto28 Feb
              • RE: insert dataGerald Preston1 Mar
            • Re: insert dataEamon Daly28 Feb
            • Re: insert dataJohn Doe1 Mar
RE: insert dataSST - Adelaide)27 Feb
  • RE: insert dataGerald Preston27 Feb
    • RE: insert dataGerald Preston27 Feb
      • RE: insert dataPaul DuBois27 Feb
RE: insert dataSST - Adelaide)27 Feb
  • RE: insert dataGerald Preston27 Feb
RE: insert dataSST - Adelaide)27 Feb
  • RE: insert dataGerald Preston27 Feb
RE: insert dataSST - Adelaide)27 Feb
RE: insert dataSST - Adelaide)1 Mar
  • RE: insert dataGerald Preston4 Mar
    • Re: insert dataRoger Baklund4 Mar
    • Re: insert dataMichael Stassen4 Mar
RE: insert dataJohn Trammell4 Mar
Re: insert dataMichael Stassen5 Mar
  • RE: insert dataGerald Preston5 Mar