SunQuest
           MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old January 29th, 2000, 10:37 AM
Teresag Teresag is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: Campbell River, BC, Canada
Posts: 3 Teresag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old February 16th, 2000, 10:42 AM
ThaGrimReaper ThaGrimReaper is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: RAF Akrotiri, Limassol Area, Cyprus
Posts: 1 ThaGrimReaper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?! URL

<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

Reply With Quote
  #3  
Old February 16th, 2000, 11:26 PM
Teresag Teresag is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: Campbell River, BC, Canada
Posts: 3 Teresag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Thank You, I'll give it a try.



[/B][/quote]


Reply With Quote
  #4  
Old February 23rd, 2000, 03:35 PM
TroutMask TroutMask is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2000
Location: Englewood, CO
Posts: 30 TroutMask User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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


Reply With Quote
  #5  
Old February 25th, 2000, 10:33 PM
Teresag Teresag is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2000
Location: Campbell River, BC, Canada
Posts: 3 Teresag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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") | | 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Help finding out if table exists in mysql


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway