我已连接到一个已在Perl中的远程服务器中托管的MySQL数据库。现在我尝试使用Perl命令行在subject.pl文件中的表上执行select语句。代码是
#!/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.
但是当我打印$ dbh变量时,它会给出DBI::db=HASH(0x1ca7884)
之类的结果。所以我猜连接正确建立。
请帮我解决这个问题,因为我是Perl脚本中的新手。
答案 0 :(得分:1)
检查prepare
上的错误,
my $sth = $dbh->prepare($sql) or die $dbh->errstr;