Nik has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use DBI; use DBD::mysql; print header( -charset=>"iso-8859-7" ); print start_html( -title=>"Ψυχωφελή Πνευματικά Κείμενα!", -background= +>"../data/images/night.jpg" ); $db = DBI->connect("DBI:mysql:nikos_db", "root", "") or $db = DBI->connect("DBI:mysql:nikos_db:50free.net", "nikos_db", "XXXXX +", {RaiseError=>1}); $db->do( "drop database if exists nikos_db"); $db->do( "create database nikos_db" );
if i want to check if database nikos_db exists is there a way to do it before the $db = DBI->connect("DBI:mysql:nikos_db", "root", "") method? because if i try to use this method first it cannot connect if the database doesnt exist! i need to check if databases exists before i try to connect to it and delete it! can you help me on that please? thanks!

Edit by castaway: X'd out possible password.

Replies are listed 'Best First'.
Re: check if databse exists before connection is made!
by Abigail-II (Bishop) on Feb 06, 2004 at 15:39 UTC
    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

      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

      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

Re: check if databse exists before connection is made!
by duff (Parson) on Feb 06, 2004 at 15:45 UTC

    If it doesn't exist you'll get undef for $db. Check the return value of your connect and do something appropriate.

Re: check if databse exists before connection is made!
by Vautrin (Hermit) on Feb 06, 2004 at 17:07 UTC
    You've answered your own question. If the $db is not defined you know that the database doesn't exist or it couldn't connect. You can rule out whether it was a problem connecting or if the database doesn't exists by checking the error string. The error will probably be in $!.
Re: check if databse exists before connection is made!
by ysth (Canon) on Feb 06, 2004 at 16:30 UTC
      is this ok to use?
      if ($db1) { $db=$db1 ); if ($db2) { $db=$db2 ); if (!$db1 and !$db2) { print font( {-size=>5, -color=>'Lime'}, "$DBI:: +errstr"; exit() );
Re: check if databse exists before connection is made!
by runrig (Abbot) on Feb 06, 2004 at 18:23 UTC
    DBI->data_sources(...) might work. Read the docs and try it.
      is this correct?
      $db1 = DBI->connect("DBI:mysql:nikos_db", "root", "") or $db2 = DBI->connect("DBI:mysql:nikos_db:50free.net", "nikos_db", "macg +yver"); if (!$db2 and $db1) { print font( {-size=>5, -color=>'Lime'}, "$DBI::e +rrstr" ); $db=$db1; } if (!$db1 and $db2) { print font( {-size=>5, -color=>'Lime'}, "$DBI::e +rrstr" ); $db=$db2; } $db->do( "drop database if exists nikos_db" ); $db->do( "create database nikos_db" ); $db->do( "use nikos_db" );
        i want my script to be able to determine if it runs on localhost or if it runs on the remote server. and also if it fails to connct to the mysql server to give the appropriate message for localhost and appropriate message for the remote web server! id the above code correct? thank you!
Re: check if databse exists before connection is made!
by techy (Scribe) on Feb 07, 2004 at 16:11 UTC
    Nik,

    You might want to use DBI->data_sources or _ListDBs to get a list of available databases for a mysql server. See Private MetaData Methods inside DBD::mysql for more information.

    If what you are looking for is to find out if the database server is running, just try to do a DBI->connect and check to see if the returned object is defined, probably with RaiseError set to 0 if you think it isn't a fatal condition.

    Regards,
    techy

Re: check if databse exists before connection is made!
by kal (Hermit) on Feb 08, 2004 at 22:32 UTC

    It seems what you're actually asking about is behaving in different ways based on different servers, rather than checking a database exists: you want one configuration on one server, and a different one on another.

    What you could do is check the contents of $ENV{'SERVER_NAME'} , and attempt to connect based on the contents of that. Maybe (it's a bit simple):

    my $db; if ($ENV{'SERVER_NAME'} eq '50free.net') { $db = DBI->connect (...); # your public server } else { $db = DBI->connect (...); # your home server } if (! defined $db) { # there was an error }
    A reply falls below the community's threshold of quality. You may see it by logging in.
    A reply falls below the community's threshold of quality. You may see it by logging in.