Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to find MySQL database exist or not

by roboticus (Chancellor)
on Sep 01, 2011 at 15:31 UTC ( #923648=note: print w/ replies, xml ) Need Help??


in reply to How to find MySQL database exist or not

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.


Comment on Re: How to find MySQL database exist or not
Download Code
Replies are listed 'Best First'.
Re^2: How to find MySQL database exist or not
by JavaFan (Canon) on Sep 01, 2011 at 15:57 UTC
    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
Re^2: How to find MySQL database exist or not
by edi (Initiate) on Sep 02, 2011 at 11:12 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-07-29 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls