|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Thank You, I'll give it a try.
[/B][/quote] |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Databases > MySQL Help > Help finding out if table exists in mysql |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|