[英]unable to execute mysql query in perl while connecting to remote server
我已經連接到一個MySQL數據庫,該數據庫已托管在Perl的遠程服務器中。 現在,我嘗試使用Perl命令行在subject.pl文件中的表上執行選擇語句。 該代碼是
#!/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;
行出現錯誤, $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腳本的新手。
檢查的錯誤prepare
,
my $sth = $dbh->prepare($sql) or die $dbh->errstr;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.