我已连接到一个已在远程服务器中托管的MySQL数据库。现在我正在尝试使用Perl命令行在subject.pl文件的表上执行select语句。该代码是无法在连接到远程服务器时在perl中执行mysql查询
#!/usr/bin/perl
use DBI;
use strict;
# Connected to mysql audit database in dev server
my $dsn = 'DBI:mysql:Driver={mysql}';
my $host = 'dev-mysql.learn.local';
my $database = 'subject';
my $user = 'testUser';
my $auth = 'testPassword';
my $dbh = DBI->connect("$dsn;host=$host;Database=$database",$user,$auth) or die "Database connection not made: $DBI::errstr";
# Prepare query
my $sql = "SELECT
subject_id
,subject_value
FROM
subject";
my $sth = $dbh->prepare($sql);
#Execute the statement
$sth->execute() or die "Unable to execute".$sth->errstr;
while (my @row = $sth->fetchrow_array()) {
my ($subject_id, $subject_value) = @row;
print "$subject_id,$subject_value,$subject_db_field\n";
}
$sth->finish();
我在行$sth->execute() or die "Unable to execute".$sth->errstr;
的错误消息是Unable to execute at D:\Demo\perl_demo\subject.pl line 24.
得到错误但当我打印$胸径变量,它给像DBI::db=HASH(0x1ca7884)
结果。所以我猜连接正在建立。
请帮我解决这个问题,因为我是全新的Perl脚本。
你检查的$ dbh-的返回值>准备($ SQL)? –
你肯定有连接,否则它会死在连接('connect ..或die ..') –
是的弗兰克,我已经检查$ dbh-> prepare($ sql)的返回值。它给'DBI :: db = HASH(0x1ca74f4)' –