2017-03-13 66 views
0

我最近發現有一個selectrow_array函數可以從數據庫中獲取值。我在使用它時遇到以下錯誤。我想知道這裏有什麼問題,找不到替代方法來做到這一點。在Perl中使用selectrow_array獲取2個值

代碼是:

my $db_connection = DBI->connect($dsn, $dbuser, $dbpassword) or die $DBI::errstr; 

my $sql_statement = "SELECT customer_id,quota FROM customer_call_quota WHERE quota>=1"; 

while (my $row = $db_connection->selectrow_array($sql_statement)) { 
    my ($cust_id, $quota) = @$row; #<---- error line 
} 

my $rc = $db_connection->disconnect ; 
return "ok"; 

錯誤:

Can't use string ("value") as an ARRAY ref while "strict refs" in use at code.pl line ... 

回答

2

兩個問題。

  • selectrow_array不返回對數組的引用。那是selectrow_arrayref
  • selectrow_*只返回第一行。

解決方案:

# Wasteful 

my $sth = $dbh->prepare($sql_statement); 
$sth->execute(); 
while (my @row = $sth->fetchrow_array()) { 
    my ($cust_id, $quota) = @row; 
    ... 
} 

my $sth = $dbh->prepare($sql_statement); 
$sth->execute(); 
while (my ($cust_id, $quota) = $sth->fetchrow_array()) { 
    ... 
} 

my $sth = $dbh->prepare($sql_statement); 
$sth->execute(); 
while (my $row = $sth->fetch()) { 
    my ($cust_id, $quota) = @$row; 
    ... 
} 
+0

啊,它只是返回的第一行。感謝你的回答! –

+0

是的,但實際上是導致錯誤的其他問題 – ikegami