http://qs1969.pair.com?node_id=426628


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

I know this could be done, but I do not know if it is the best way. All of the mysql tables are stored in a directory as tablen_name.file_extension. I am not sure if the file extension is the same for every table type or not (mine are *.ISDm, *.ISM and *.frm). Something liek this (minimally tested)

## Code is to be considered untested. ## This assumes that each *$i* has only one table name and no other ch +aracters in it $mysql_table_path = "Your path to your databases"; $dbname="The name of the database"; $db_extension="The file extension for your database type"; for $i (0 ..$#tables_to_query) { $filepath = "$mysql_table_path/$dbname/" . $tables_to_query[$i] ." +.$db_extension"; if (-e $filepath) { $sql = "select count(*) from ".$tables_to_query[$i].";"; $sth = $dbh->prepare($sql) or die("Could not prepare!" . $dbh +->errstr); $sth->execute() or die("Could not execute!" . $dbh->errstr); $total = $sth->fetchrow_array(); print "Total $i is $total\n"; } }

You can find the path to the db directory in the configuration file, either "/etc/mysql/my.cnf" or "/etc/my.cnf". If might be somewhere else, but I have always found it in one of these places. The line you want is the "datadir=" line. If all of your tables are the same database type, then they should all have the same extensions (as far as I know).

[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock [mysql.server] user=mysql basedir=/var/lib [safe_mysqld] err-log=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid

It works for me on my system, but I do not have the number of tables that you have, and probably not the same setup. This is obviously black magic, as I know of no guarentee that the tables will always be exposed like this, but it it does work for me.

Otherwise, your best bet (and the best solution) sounds like you need to trap the errors.