DBD::mysql - Problem with dropping a database
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, @database开发者_运维百科s );
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
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.
精彩评论