in reply to MySQL Database Check...

The 'SHOW TABLES' MySQL command will return a list of all the tables in your database. No error checking for brevity. Obviously you can just load this into a hash for a quick lookup.

require DBI; my $dbh = DBI->connect( ..blah.. ); my $sth = $dbh->prepare('SHOW TABLES'); $sth->execute(); while (my ($table) = $sth->fetchrow_array ) { print "$table\n"; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: MySQL Database Check...
by steves (Curate) on Jan 02, 2003 at 05:29 UTC

    Maybe this works in mySQL, but as a general rule it does not work in DBI because SHOW TABLES is not SQL. I use Oracle and in Oracle, SHOW TABLES is a SQL*Plus command but does not work via DBI prepare/execute calls. I believe this is why the DBI table methods were added -- to insulate these database specifics and allow table queries to exist across all databases in a consistent manner.

      I did specify it was MySQL specific (and yes it does work ;-) but as you state using the DBI table methods or even just wrapping in an eval or changing RaiseError will be more portable options.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Well, thanks guys. After I posted, I was playing around with it, and I remembered a few things...

        Here is how I got it to work.

        $sth = $dbh1->selectrow_array ( "SELECT * FROM `some_table_name` LIMIT 1"); if ($dbh1->errstr) { #Does not exist } else { # DOES exist }

        I used LIMIT 1 in case it does exist and has thousands of entries. This way it will be faster, if it does exist. Otherwise it don't matter.

        It worked for what I was wanting it to do.

        thx,
        Richard
      While you are 100% correct steves, the OP did specifically state that this would be for MySQL. In which case tachyon was not wrong in mentioning 'SHOW TABLES' IMHO.

      -- vek --