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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on how to know if a databse exists before i delete it?

Replies are listed 'Best First'.
Re: how to know if a databse exists before i delete it?
by Caron (Friar) on Feb 05, 2004 at 18:26 UTC

    Assuming (from your example) that you are talking about MySQL, there are two ways:

    # easy $db->do("DROP DATABASE IF EXISTS nikos_db"); # difficult my $dbs = $db->selectcol_arrayref("show databases") or die("can't get the db list\n"); if (grep {$_ eq 'nikos_db'} @$dbs) { $db->do("DROP DATABASE nikos_db"); }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to know if a databse exists before i delete it?
by Zaxo (Archbishop) on Feb 05, 2004 at 18:22 UTC

    See &DBI::data_sources. It can be called before you even have a DBI handle.

    After Compline,
    Zaxo

Re: how to know if a databse exists before i delete it?
by calin (Deacon) on Feb 05, 2004 at 20:27 UTC
    A few comments on portability (as you did not specify which DB engine you were using)

    1. Dropping a database

    The SQL92 standard does not specify a DROP DATABASE statement AFAIK (can somebody please confirm/infirm this as I don't have access to standard documents, also comments on newer standards would be welcome). This is no big deal anyway, because most DBs stick to their fancy syntax, mainly for backward compatibility reason, and because vendors think that sticking with it will give them competitive edge/distinct personality/whatever. The two mature/widespread free software engines (PostgreSQL and MySQL) implement it though. Also the standard DBI module does not implement abstraction for dropping a database ; there are AFAIK "higher level" modules on CPAN that do it but I'm not familiar with them, if you're interested do a search. Your best friend is the standard documentation for your DB, anyway.

    2. Listing databases

    This is even more non-standard, but fortunately DBI has abstraction for it from version 1.38 (if your DB engine concept of a "data base" matches DBI's concept of a "data source", which is usually the case with backend DBs). The method is called data_sources.

    To give you an idea of this biodiversity, this is how I would solve your problem for PostgreSQL with only native SQL (PostgreSQL doesn't implement DROP DATABASE ... IF EXISTS) :

    use DBI; my $dbh=DBI->connect('dbi:Pg:dbname=template1', 'user' , 'password', { + RaiseError => 1, AutoCommit => 1 } ); # [...] sub pg_drop_if_exists { my $dbname = shift; my $sth = $dbh->prepare(q{SELECT datname FROM pg_catalog.pg_database + WHERE datname = ?}); $sth->execute($dbname); if ($sth->fetchrow_arrayref()) { $dbh->do('DROP DATABASE ' . $dbname); #placeholder and quote don't + work, security! } } pg_drop_if_exists('nikos_db');
Re: how to know if a databse exists before i delete it?
by Anonymous Monk on Feb 06, 2004 at 08:55 UTC
    Why would you care (makes no difference if it exists or not, just do)?
    A reply falls below the community's threshold of quality. You may see it by logging in.