| 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 data | Gerald Preston | 27 Feb |
| • Re: insert data | John Doe | 27 Feb |
| • RE: insert data | Gerald Preston | 27 Feb |
| • Re: insert data | John Doe | 28 Feb |
| • Re: insert data | Michael Stassen | 28 Feb |
| • Re: insert data | Michael Stassen | 28 Feb |
| • RE: insert data | Gerald Preston | 28 Feb |
| • RE: insert data | William R. Mussatto | 28 Feb |
| • RE: insert data | Gerald Preston | 1 Mar |
| • Re: insert data | Eamon Daly | 28 Feb |
| • Re: insert data | John Doe | 1 Mar |
| • RE: insert data | SST - Adelaide) | 27 Feb |
| • RE: insert data | Gerald Preston | 27 Feb |
| • RE: insert data | Gerald Preston | 27 Feb |
| • RE: insert data | Paul DuBois | 27 Feb |
| • RE: insert data | SST - Adelaide) | 27 Feb |
| • RE: insert data | Gerald Preston | 27 Feb |
| • RE: insert data | SST - Adelaide) | 27 Feb |
| • RE: insert data | Gerald Preston | 27 Feb |
| • RE: insert data | SST - Adelaide) | 27 Feb |
| • RE: insert data | SST - Adelaide) | 1 Mar |
| • RE: insert data | Gerald Preston | 4 Mar |
| • Re: insert data | Roger Baklund | 4 Mar |
| • Re: insert data | Michael Stassen | 4 Mar |
| • RE: insert data | John Trammell | 4 Mar |
| • Re: insert data | Michael Stassen | 5 Mar |
| • RE: insert data | Gerald Preston | 5 Mar |
