-1

我有一个这样的事务表:

TransactionID    Date           AccountNumber   Type    Amount  
657520           02-07-1999     016901581432    Debit   16000  
657524           02-07-1999     016901581432    Debit   13000  
657538           09-07-1999     016901581432    Credit  11000  
657548           18-07-1999     016901581432    Credit  15500  
657519           02-07-1999     016901581433    Debit   12000  
657523           02-07-1999     016901581433    Credit  11000  
657529           03-07-1999     016901581433    Debit   15000  
657539           10-07-1999     016901581433    Credit  10000  
657541           11-07-1999     016901581434    Debit   12000  
657525           03-07-1999     016901581434    Debit   15000  
657533           05-07-1999     016901581434    Credit  12500 

我应该使用数据库中的 SQL 查询找到每个帐户的总借方和总贷方金额,并使用 Perl 将结果存储在电子表格中。

这是我的代码:

#!/usr/bin/perl
use DBI;
use strict;
use warnings;
use Spreadsheet::WriteExcel;
$dbh = DBI->connect('dbi:database','prithvi','prithvi') or die "Couldn't connect";
my $tran_cur = $dbh->prepare("SELECT AccountNumber, Type, SUM(Amount) FROM transaction GROUP BY AccountNumber, Type");
my $workbook = Spreadsheet::WriteExcel->new('results.xls');
my $worksheet = $workbook->add_worksheet('Result');
my $row = 0;
my $col = 0;
$worksheet->write_row($row++, $col, ['Account Number','Type','Total Amount']);
while( my @data = $tran_cur->fetchrow_array)
{
 $worksheet->write_row($row++, $col, \@data);
}

我在代码中哪里出错了?请帮忙。我只得到 Excelsheet 中的标题作为输出。

4

1 回答 1

2

您需要在调用之前执行准备好的语句$tran_cur->fetchrow_array

$tran_cur->execute;
于 2012-10-06T05:22:54.773 回答