tags:

views:

48

answers:

1

Hello! Why doesn't the line "$rc = $dbh->func( 'dropdb', $dbname, 'admin' );" remove the database db_test_2?

#!/usr/bin/perl
use warnings;
use strict;
use 5.010;
use DBI;

my $host = 'localhost';
my $user = 'user';
my $password = 'password';
my( $rc, $dbname, @databases );



my $drh = DBI->install_driver( 'mysql' );
$dbname = 'db_test_1';

# use a driver handle (drh)

$rc = $drh->func( 'createdb', $dbname, $host, $user, $password, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_1
# DBI:mysql:mysql


$rc = $drh->func( 'dropdb', $dbname, $host, $user, $password, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:mysql



my $dbh = DBI->connect( "DBI:mysql:", $user, $password, { RaiseError=>1, AutoCommit=>1 } );
$dbname = 'db_test_2';

# reuse the existing connection of a database handle (dbh)

$rc = $dbh->func( 'createdb', $dbname, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_2
# DBI:mysql:mysql

$rc = $dbh->func( 'dropdb', $dbname, 'admin' );
say for DBI->data_sources( $driver, { host => $host, user => $user, password => $password });
# DBI:mysql:information_schema
# DBI:mysql:db_test_2
# DBI:mysql:mysql
+3  A: 

It is interesting, in the DBI func() it is defined differently, but DBD::MySQL has a convince method to it for what is really an _admin_internal method, which is defined in Mysql.xs and looks to have quite a few calls to do_error(), which leads me to believe that you should check the errors. Try connecting with RaiseError => 1 and see what it says.

To do this, connect with

  $dbh = DBI->connect("DBI:mysql:database=$db;host=$host",
                      $user, $password, {RaiseError => 1});

then use $dbh->func('dropdb' ...), and see what happens.

Evan Carroll
I don't know, if this is the right way to check for errors, but when I write "$dbh->func( 'dropdb', $dbname, 'admin' );say $dbh->{'mysql_error'};"I get "MySQL server has gone away"; but I don't know why he has gone away.
sid_com
If you're doing it my way, I would make the next thing I check the mysql logs when the server is running in super duper crazy verbose mode (however you do that).
Evan Carroll
I don't know if I had understood you in the right way but in my DBI-scripts RaiseError is always on. The thing I found out is that it is not only the "dropdb" that lets the server go away, actually in this example it is the "createdb". But maybe this is a feature.Since there are other ways to create and drop databases this should not be a big problem for me apart from the unsatisfied curiosity.
sid_com