Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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)}


In reply to DBI: checking if a table exists in a cross-DB manner? by radiantmatrix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found