in reply to check if databse exists before connection is made!

Normally, you can't check to see if a database is there before making a connection. Remember that you make a connection to a database server, one that has zero or more databases available. It's the same that you cannot determine whether a URL exists without contacting the server; you can't determine whether an email address exists without asking the appropriate server; and you can't determine whether you still have ice-cream without opening the freezer.

There are of course exceptions. If you have access to the machine the MySQL server runs on, you have the appropriate permissions, you know where the server keeps its datafiles, and you know the structure of said files (and directories), you can find out. You also might be able to find out whether the database exists by peeking in the appropriate log file.

Abigail

  • Comment on Re: check if databse exists before connection is made!

Replies are listed 'Best First'.
Re: check if databse exists before connection is made!
by Anonymous Monk on Feb 09, 2004 at 05:31 UTC
    you can't determine whether you still have ice-cream without opening the freezer.

    I beg to differ. What if you happen to have a freezer with a transparent door? I could very well see if there is ice cream without opening it. ;)

      Only if you defrost the freezer daily. Otherwise, the inside will be frosted up, and you can't see through it. Since you post here, you are either a nerd or a geek, or both, and nerds and geeks don't defrost their freezers daily.

      Besides, you never fixed the broken light in the freezer, and even after you fixed it, you don't know whether there's ice-cream hiding behind the frozen spinach.

      Abigail

        But a true geek might give the ice cream its own NIC so he/she can ping it. Or even SNMP-enable it so he can trap "out of ice cream" conditions.

Re: Re: check if databse exists before connection is made!
by Nik (Initiate) on Feb 06, 2004 at 15:47 UTC
    so what can i do about it now? this scripts fails to execute if the database doesnt exist!! so its not working properly! what can i do to fix it?
      Uhh .. errr ... uhhh ... check for return values and do what you need to? Basically, you're doing a bunch of stuff without checking return values. Also, you have RaiseError set on one of your connects so you could wrap the code inside an eval.

      my $dbh = DBI->connect( $dsn, $user, $password, { RaiseError => 1 } ); if( ! $dbh ) { print "Ugg, connection problem: $DBI::errstr\n"; } else { eval { $dbh->do( whatever ); $dbh->do( something else ); }; if( $@ ) { print "Ugg, problem: $@\n"; } }
      -derby
      Either of two things: check the return value of the connect method, and if it fails, check the reason. Or connect to a database you know exists, and once connected, check to see whether the database you are interested in exists.

      Abigail