PERL :: DBI在列表中获取用户数据库

时间:2014-10-21 06:54:15

标签: mysql perl dbi

我正在尝试创建一个小脚本来从我的数据库中提取数据库/表/列,但在第一步中我无法继续,我在奇怪的列表中获取数据库,请查看:

#!/usr/bin/perl
use DBI;

$host = "localhost";
$user = "wnyclick_siteusr";
$pw = "Hank0402\$";

$dsn = "dbi:mysql:$database:localhost:3306";
$connect = DBI->connect($dsn, $user, $pw);
$databases = $connect->selectcol_arrayref('show databases');

use Data::Dumper;
print Dumper $databases;

执行此代码,为我提供以下内容:

$VAR1 = [
          'information_schema',
          'wnyclick_sitedatawp'
        ];

如何将此执行结果放在列表中?

print @VAR1[0];
print @databases[0];

1 个答案:

答案 0 :(得分:2)

我刚修改了你的代码。请尝试以下代码:

#!/usr/bin/perl -w
use DBI;
use DBD::mysql;

my $user = "wnyclick_siteusr";
my $pw   = "Hank0402\$";

#Connecting Database
$dbh = DBI->connect( 'dbi:mysql:database=mysql;host=localhost;port=3306', '$user', '$pw' )
    or die "Connection Error: $DBI::errstr\n";
$sql = "show databases";
$sth = $dbh->prepare($sql);
$sth->execute or die "SQL Error: $DBI::errstr\n";

while ( @row = $sth->fetchrow_array ) {
    #print $row[1];
    print "@row\n";
}