表中的Perl数据库脚本
可能重复: 我有这样一个事务表:表中的Perl数据库脚本,perl,Perl,可能重复: 我有这样一个事务表: TranID Date AccNum Type Amount ChequeNo DDNo 657520 02-07-1999 0181432 Debit 16000 465774 657524 02-07-1999 0181432 Debit 13000 569086 657538 09-07-1999 0181432 Credit 11000 657548 18-
TranID Date AccNum Type Amount ChequeNo DDNo
657520 02-07-1999 0181432 Debit 16000 465774
657524 02-07-1999 0181432 Debit 13000 569086
657538 09-07-1999 0181432 Credit 11000
657548 18-07-1999 0181432 Credit 15500
657519 02-07-1999 0181432 Debit 12000
657523 02-07-1999 0181432 Credit 11000
657529 03-07-1999 0181433 Debit 15000 466777
657539 10-07-1999 0181433 Credit 10000
657541 11-07-1999 0181433 Debit 12000
657525 03-07-1999 0181433 Debit 15000 569999
657533 05-07-1999 0181433 Credit 12500
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print "content-type:text/html\n\n";
my $dbh = DBI->connect('dbi:mysql:database:3306','prithvi','prithvi')
or die "Couldn't connect";
my $tran_cur = $dbh->prepare("SQL Query");
$tran_cur->execute;
map { print "<td>$_</td>" } qw(AccountNumber-ChequeDebit-DDDebit-CashDebit);
print "<br/>";
while (my @data = $tran_cur->fetchrow_array()) {
my $rec = join '-', @data;
print "$rec<br/>";
}
$tran_cur->finish;
$dbh->disconnect;
我的问题是:
查询交易表中的数据,计算每个账户的支票、dd和现金借记总额。
我的代码如下:
TranID Date AccNum Type Amount ChequeNo DDNo
657520 02-07-1999 0181432 Debit 16000 465774
657524 02-07-1999 0181432 Debit 13000 569086
657538 09-07-1999 0181432 Credit 11000
657548 18-07-1999 0181432 Credit 15500
657519 02-07-1999 0181432 Debit 12000
657523 02-07-1999 0181432 Credit 11000
657529 03-07-1999 0181433 Debit 15000 466777
657539 10-07-1999 0181433 Credit 10000
657541 11-07-1999 0181433 Debit 12000
657525 03-07-1999 0181433 Debit 15000 569999
657533 05-07-1999 0181433 Credit 12500
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print "content-type:text/html\n\n";
my $dbh = DBI->connect('dbi:mysql:database:3306','prithvi','prithvi')
or die "Couldn't connect";
my $tran_cur = $dbh->prepare("SQL Query");
$tran_cur->execute;
map { print "<td>$_</td>" } qw(AccountNumber-ChequeDebit-DDDebit-CashDebit);
print "<br/>";
while (my @data = $tran_cur->fetchrow_array()) {
my $rec = join '-', @data;
print "$rec<br/>";
}
$tran_cur->finish;
$dbh->disconnect;
提前谢谢。请提供帮助。请尝试以下代码:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print "content-type:text/html\n\n";
my $dbh = DBI->connect('dbi:mysql:database:3306','prithvi','prithvi')
or die "Couldn't connect";
my $tran_cur = $dbh->prepare("
SELECT `AccNum`, `Amount`
FROM `database`
WHERE `Type` = 'Debit'
ORDER BY `TranID`;
");
$tran_cur->execute;
print "<br>AccountNumber-ChequeDebit-DDDebit-CashDebit</br>\n";
my %h;
while (my @data = $tran_cur->fetchrow_array()) {
if ($h{$data[0]}) {
$h{$data[0]} .= "-$data[1]";
}
else {
$h{$data[0]} = $data[1];
}
}
$tran_cur->finish;
$dbh->disconnect;
foreach my $key (sort keys(%h)) {
print "<td>$key" . "-" . $h{$key} . "</td>\n";
}
它是完美的,但它是有效的 这不是其他问题的重复。问题是不同里德:这与SO 12756971有关,但不同。您应该编辑脚本以适合您想要的现实生活HTML表格您所说的语句$h{$data[0]}是什么意思?=-$data[1];这句话对我来说是新的吗?它很关键:一个散列(见Callaed associative array):一种用一些单词作为索引的数组。你应该掌握数据类型,这是一个良好开端的要求。我是说散列,数组,标量…@user1708526,你为什么不接受这个答案?