DBD :: mysql - 删除数据库的问题

时间:2010-01-18 19:14:30

标签: mysql perl

为什么不行“$ rc = $ dbh-> func('dropdb',$ dbname,'admin');”删除数据库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

1 个答案:

答案 0 :(得分:3)

有趣的是,在DBI func()中它的定义不同,但是DBD :: MySQL对于它真正的_admin_internal方法有一个conince方法,它在Mysql.xs中定义并且看起来对do_error()进行了很多调用,这让我相信你应该检查错误。尝试与RaiseError => 1联系并查看其内容。

要执行此操作,请使用

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

然后使用$dbh->func('dropdb' ...),看看会发生什么。