#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Campbell River, BC, Canada
    Posts
    3
    Rep Power
    0
    In a Perl CGI script working with Mysql, I am trying to write:

    if a table does not exist, create it;

    How do I find out if a table exists? What value can I capture for my if statement?
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    RAF Akrotiri, Limassol Area, Cyprus
    Posts
    1
    Rep Power
    0
    Try using SELECT to return anything. If $rv->rows = 0 then take it as red that it doesn't exist and create it.

    This help?!

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Teresag:
    In a Perl CGI script working with Mysql, I am trying to write:

    if a table does not exist, create it;

    How do I find out if a table exists? What value can I capture for my if statement?
    [/quote]



    ------------------
    http://www.hawkerdesign.com
    Craig Hawker,
    Chief Designer
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Campbell River, BC, Canada
    Posts
    3
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Thank You, I'll give it a try.



    [/B][/quote]

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2000
    Location
    Englewood, CO
    Posts
    30
    Rep Power
    15
    That will tell you if a table has any rows, but not if the table exists. I would use SHOW TABLES to get the list of tables, then iterate over that list to see if the table name I am looking for exists.

    If you see that the row count = 0, but the table exists with 0 rows, you will get an error if/when you try to CREATE that table.

    -TM

  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2000
    Location
    Campbell River, BC, Canada
    Posts
    3
    Rep Power
    0
    In the end, I did the following:

    #Check if table exists, if not then create it
    my $sth = $dbh->prepare("SELECT * FROM office") &#0124; &#0124; die "Can't prepare a statement: $DBI::errstr";

    my $rv = $sth->execute; #if table doesn't exist undef is returned
    if($rv eq undef)
    {
    $sth = $dbh->do(q(
    CREATE TABLE ....
    }

    Once I found out that if the table doesn't exist then undefined is returned, then the following if statement worked successfully.
    Thank you for your thoughts on this, I will keep them handy for future use.

Similar Threads

  1. Replies: 3
    Last Post: January 28th, 2010, 03:14 PM
  2. Finding unused integer with MySQL
    By haravikk in forum MySQL Help
    Replies: 1
    Last Post: January 28th, 2004, 12:13 PM
  3. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 07:19 PM
  4. test for mySQL table
    By Splendid_Bob in forum PHP Development
    Replies: 4
    Last Post: July 12th, 2002, 08:50 AM
  5. mysql table join hell
    By Soylent-0 in forum PHP Development
    Replies: 1
    Last Post: June 26th, 2001, 01:45 PM

IMN logo majestic logo threadwatch logo seochat tools logo