Perl - DBI :: CSV排序问题

时间:2011-08-24 08:34:35

标签: perl dbi

我有一个名为x.txt的文本文件,其中包含以下数据:

emailid,hits
aa,100
bb,200
cc,300
dd,400
ee,500
aa,400

我的perl代码是

use DBI;
$dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".",csv_sep_char     => "," });

my $query = "SELECT emailid,sum(hits) tothits FROM x.txt  group by emailid order by tothits desc";
my $sth   = $dbh->prepare ($query);
$sth->execute ();
while (my $row = $sth->fetchrow_hashref) {
    print $row->{emailid},"--",$row->{tothits},"\n";

    }
$sth->finish ();

返回

cc--300
bb--200
dd--400
aa--500
ee--700

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

您的SQL看起来没问题。排序没有发生的原因似乎是因为limitations in DBD::CSV

我试图看看它是否适用于DBD :: AnyData,但是它无法识别tothits并且死亡。

看起来你需要做一个解决方法,比如fetchall然后排序。