2012-10-06 147 views
-1

數據庫在Perl我有一個事務表是這樣的:從電子表格

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中獲得標題作爲輸出。

+1

哪裏是你的'$ tran_cur-> execute'? –

回答

2

你需要調用$tran_cur->fetchrow_array之前執行準備的語句:

$tran_cur->execute;