在 Perl 中,我正在执行类似于以下的 SQL 查询:
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++;
}
我尝试在 Google 上四处寻找,但找不到内置于 DBI/Perl 中的许多好的答案。我确实找到了一个提供此功能的开源类,但我觉得这应该可以在不使用其他人的类的情况下实现。
最佳答案
fetchrow_hashref
呢? ?
关于mysql - 在 Perl 中将 MySQL 结果作为哈希表返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7788704/