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

Hi, I've posted this also on comp.lang.perl.tk, but am also asking here, because I'm guessing this is more of a Perl problem than Tk.

Anyways, what I need to do is test my sql statement in a prepare statement, and stop executing the Tk callback if an error is raised. The error is plainly raised, but I can't seem to figure out how to have it set a flag, so I can return out of the callback before executing the sql.

$entrysql->bind('<Return>',[sub { $sql = $entrysql->get; my $sth; eval {$sth = $dbh->prepare("select $mainstr from info $sql") or warn DBI::errstr $!; $sth->finish; }; if ($@){ print "ERROR - $@\n"; print chr(07); return; } .......continue to execute the sql }
When I enter a malformed sql statement in the above, I get the following printed to STDOUT. But the $@ is never caught in the eval, and it continues on as if there was no error.
DBD::SQLite::st execute failed: near "=": syntax error at ./ztkdb12a l +ine 664. Tk::Error: dbih_setup_fbav: invalid number of fields: 0, NUM_OF_FIELDS + attribute probably not set right at ./ztkdb12a line 668. <Key-Return> (command bound to event)

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: $DBI::errstr and Tk:Error
by jeffa (Bishop) on Mar 11, 2004 at 18:03 UTC
    Are you setting RaiseError to true when you connect to the database? If you are, try turning it off. Also, you might be able to use HandleError to your advantage by having it pass the buck to Tk.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: $DBI::errstr and Tk:Error
by Roy Johnson (Monsignor) on Mar 11, 2004 at 18:22 UTC
    Why aren't you die-ing in the eval? warn doesn't return an error status.

    The PerlMonk tr/// Advocate
Re: $DBI::errstr and Tk:Error
by zentara (Cardinal) on Mar 12, 2004 at 01:03 UTC
    Ok, I solved my problem. The problem was I erroneously thought I could get a DBI::errstr based on the $sth prepare statement alone....you need an execute too. doh!! So for the benefit of others looking for an answer, here is my code which works fine:
    ##################################################### $entrysql->bind('<Return>',[sub { $sql = $entrysql->get; eval { local $dbh->{AutoCommit} = 0; my $sth = $dbh->prepare("select $mainstr from info $sql") or die DBI::errstr; $sth->execute or die DBI::errstr; }; if($@){ print chr(07), $@, "bad sql\n"; return} print "seems ok\n"; #....continue on and do it for real

    I'm not really a human, but I play one on earth. flash japh
Re: $DBI::errstr and Tk:Error
by zentara (Cardinal) on Mar 11, 2004 at 19:42 UTC
    Thanks for those tips, I will check them out and see what effect they have. In the mean time, I hacked together a solution myself.....it seems to work, but only more testing will tell. ;-)
    #Set a global my $sqlerror = undef; $entrysql->bind('<Return>',[sub { $sql = $entrysql->get; my $sth; eval {$sth = $dbh->prepare("select $mainstr from info $sql") #or warn DBI::errstr $!; or Tk::Error $!; $sth->finish; }; if($sqlerror){print chr(07);$sqlerror = undef; return;} print "ok\n"; .....continue on ######################################################## sub Tk::Error { (undef,$sqlerror,undef) = @_; print "my error->$sqlerror\n"; }

    I'm not really a human, but I play one on earth. flash japh