DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, My code is generating a series of sql statements where one of them is used in the end. One of them is faulty...instead of manually searching for the statement that is incorrect, can I instead "fetch" the statement from the dbh object when the error occurs?

E.g.:
$sth->execute() or warn ("Couldn't execute statement - ???");

Replies are listed 'Best First'.
Re: DBI - check erroneous statement?
by marto (Cardinal) on Nov 19, 2010 at 15:55 UTC

    Are you not checking the return status?

    my $rv = $sth->execute() or die $sth->errstr;

    This should throw an error and indicate where the problem with the SQL is.

    Update: For more information see the DBI documentation.

      Ok, but that gives me only a hint of where the problem can be ("check the manual that corresponds to your MySQL server version for the right syntax to use near"). Is it possible to see the entire statement?

        DBD::Oracle displays the SQL and points out where it thinks the error is. Perhaps look at DBD::mysql (search for RaiseError => 1) or the DBI documentation on Tracing.

        Can't you just examine $sth->{Statement}? See Statement

Re: DBI - check erroneous statement?
by fisher (Priest) on Nov 19, 2010 at 15:50 UTC
    I think you mean something like
    $sth->execute() or warn ("execute error: $DBI::errstr");
Re: DBI - check erroneous statement?
by erix (Prior) on Nov 19, 2010 at 21:11 UTC
Re: DBI - check erroneous statement?
by Anonymous Monk on Nov 20, 2010 at 22:21 UTC
    Hi,

    I just used to print every SQL statement to the log, along with any values that where going to be put into placeholders.

    Or you could print them out in your error message.

    J.C.