in reply to Re: Best Perlish way to test if a mysql table exists?
in thread Best Perlish way to test if a mysql table exists?

eval is only necessary if DBI's RaiseError flag was set on connection to the database; otherwise $dbh->err must be checked. This should probably be done with a query such as SELECT 1 FROM table WHERE 0 which would always succeed but would never return any results, provided the table exists.

Makeshifts last the longest.

  • Comment on Re^2: Best Perlish way to test if a mysql table exists?

Replies are listed 'Best First'.
Re^3: Best Perlish way to test if a mysql table exists?
by punch_card_don (Curate) on Jan 31, 2005 at 01:14 UTC
    Just read up on RaiseError - maybe a possibility. But that would create an error trapping nightmare for me having to re-write trappig code for every line of sql in the script....

    Oh, for a "if table exists" function!

    Forget that fear of gravity,
    Get a little savagery in your life.

      To be honest, I don't think such functionality is particularly warranted. Constantly checking for the existence of a table smacks of bad design. Sometimes we are stuck with bad design, of course.

      Makeshifts last the longest.

      re-write trappig code for every line of sql in the script....
      Not necessary, you can set RaiseError for just that particular query. BTW, did you know you can localize hash/array values? Even if the hash/array is a lexical variable?
      eval { local $DBH->{RaiseError} = 1; local $DBH->{PrintError} = 0; $sth = $DBH->prepare($sql); $sth->execute; 1; }; # table was found if $@ is empty
      Alternately, you can just set RaiseError/PrintError on the individual statement handle (not the database connection handle).

      blokhead

        Except it wasn't the point to set that for a single query. It's usually best to set RaiseError on the connection handle and wrap larger blocks of calls with an eval; that frees you from the responsibility of constantly checking $dbh->err. If you aren't already using RaiseError, I don't see how going through all the trouble to enable it for one query is better than the regular $dbh->err check.

        Makeshifts last the longest.