http://qs321.pair.com?node_id=411923

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

I am aware that many RDBMS packages provide a mechanism to check for the existance of a table via SQL. I've also read if table exists (DBI) on the subject, but it does not provide an answer for me. I need to examine whether or not a table exists in a way that is portable to all (or at least most) DBI-able RDBMS.

At least, that's what I think I need to do... My logic looks like this in psuedo-code:

sub _recreate_table { my $table_name = shift; drop_table($table_name) if table_exists($table_name); create_table($table_name, %schema); }

The reason for this is that I need to purge all records from a table, and then insert data from a flat file into a cleanly-created table. When the files change, %schema changes as well. DELETE FROM $table_name is too slow (approx 1.5M records per table).

This works, but assumes that any error means the table just didn't exist:

$dbh->{RaiseError} = 1; eval { $sth->do("DROP TABLE $table_name") }; if ($@) { ## Do nothing }

That's a Bad Thing™, IMO, partly because I'd like to provide the option of prompting the user with something like "This table exists, are you sure you wish to drop it?" when the table is already there. Any ideas on a better way to approach this challenge, and one that will work with pretty much any DB supported by DBI?

radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}