Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

How to find MySQL database exist or not

by edi (Initiate)
on Sep 01, 2011 at 15:12 UTC ( #923645=perlquestion: print w/ replies, xml ) Need Help??
edi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, How to check whether a MySQL database exist or not?
use DBI; eval { $dbh = DBI->do('dbi:mysql: xxxxx','root','') or die "Connection Error: $DBI::errstr\n"} ; if ($dbh == 'undef') { print "Table does not exist.\n"; } else { print "table exist"; }
Thank you in advance!

Comment on How to find MySQL database exist or not
Download Code
Re: How to find MySQL database exist or not
by roboticus (Canon) on Sep 01, 2011 at 15:31 UTC

    edi:

    I'd make the following change:

    if (defined $dbh) { print "Table does not exist.\n"; }

    The version you have checks whether $dbh is 0. (Had you used eq instead of ==, it would have checked whether $dbh contained the string 'undef'.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I would not make the same change. Why bother wrapping an "X or die" inside an eval, then check whether X is false afterwards?
      if ($dbh = DBI->do('dbi:mysql: xxxxx','root','')) { print "Table exists\n" } else { print "Table does not exists"; }
      Of course, all it checks is whether one can connect - not whether a database exists (title question) or whether a table exists (message printed).

        JavaFan:

        Good point. I just saw the $blah == 'undef' and was immediately horrified.

        ;^)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        And, once we are at nitpicking, it checks it after the program starts, not before (per spec). :)
        thank you JavaFun for the reply. but it is giving an error as follows: Name "main::dbh" used only once: possible typo at C:/Program Files..... Can't locate auto/DBI/do.al in @INC
      use DBI; eval { $dbh = DBI->do('dbi:mysql:xxxxxxx','root','') or die "Connection Error: $DBI::errstr\n" } ; if (defined $dbh) { print "Table does not exist.\n"; } else { print "Table exist";}
      code will print "Table exist" whether the database exist or not. Please help.
        I think that you are mixing up a couple of things.

        There is a dataset name ($dsn) which is a mySQL database on a particular host name and port number. That has to exist or you are going "nowhere"!

        If the $dsn exists, then you can attempt to connect to it. This is where the user name and passwords are required.

        my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port" || die "DB data set name failed $!\n"; # # second step # # my $dbh = DBI->connect($dsn, $user, $password) || die "DB connect failed $!\n";
        The $dsn and the final connected handle, $dbh are very different things.

Re: How to find MySQL database exist or not
by rcaputo (Chaplain) on Sep 01, 2011 at 17:46 UTC

    Query MySQL's internal tables for the information:

    1) macbookpoe:~% mysql -BN -e 'select count(1) from information_schema +.TABLES where TABLE_SCHEMA="test" and TABLE_NAME="moo"' 1 1) macbookpoe:~% mysql -BN -e 'select count(1) from information_schema +.TABLES where TABLE_SCHEMA="test" and TABLE_NAME="moue"' 0

Re: How to find MySQL database exist or not
by Anonymous Monk on Sep 02, 2011 at 11:41 UTC
Re: How to find MySQL database exist or not
by Corion (Pope) on Sep 02, 2011 at 12:50 UTC

    Have a look at the Catalog Methods of DBI. With these, you can easily find out what databases, tables and columns exist.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://923645]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-04-20 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls