在Perl中,我正在编写类似于以下的SQL查询语句:
我希望的是将此查询结果转储到散列表中。我正在使用 DBI,目前只会执行以下操作:
SELECT `id`, `title`, `price` FROM `gamelist`
我希望的是将此查询结果转储到散列表中。我正在使用 DBI,目前只会执行以下操作:
my %results;
my $count = 0;
while( @result = $statement->fetchrow() ){
%results{'id'}[$count] = $result[0];
%results{'title'}[$count] = $result[1];
%results{'price'}[$count] = $result[2];
$count++;
}
然而我不喜欢使用$result[0]
,并相信第一个字段就是ID。我更愿意有这样的东西:
my %results;
my $count = 0;
while( %result = $statement->fetchrow_as_hashtable() ){
%results{'id'}[$count] = %result{'id'};
%results{'title'}[$count] = %result{'title'};
%results{'price'}[$count] = %result{'price'};
$count++;
}
我在谷歌上搜索了很久,但没有找到DBI/Perl相关的好答案。我确实找到了一个开源类,提供了这个功能,但是我觉得应该不需要使用别人的类就能实现。