Perl DBI fetchall_hashref

emx*_*emx 8 mysql perl hash dbi

请考虑下表:

mysql> select * from vCountryStatus;
+-------------+------------+------+---------+--------+-----------------+
| CountryName | CountryISO | Code | Status  | Symbol | CurrencyName    |
+-------------+------------+------+---------+--------+-----------------+
| Brazil      | BR         |   55 | LIVE    | BRL    | Brazilian Real  |
| France      | FR         |   33 | offline | EUR    | Euro            |
| Philippines | PH         |   63 | LIVE    | PHP    | Philippino Peso |
+-------------+------------+------+---------+--------+-----------------+
3 rows in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

我正在尝试基于此表构造哈希.为此我做了以下事情:

#!/usr/bin/perl

use DBI;
use Data::Dumper;

my $dbh = DBI->connect("dbi:mysql:database=db", "user", "password", {RaiseError => 1, AutoCommit => 0, FetchHashKeyName => "NAME_lc"}) || die "DB open error: $DBI::errstr";

my $sth = $dbh->prepare("select * from vCountryStatus");
$sth->execute;
my $hash = $sth->fetchall_hashref('countryiso');
print Dumper($hash);
Run Code Online (Sandbox Code Playgroud)

这是生成的输出:

$VAR1 = {
          'PH' => {
                    'symbol' => 'PHP',
                    'status' => 'LIVE',
                    'countryname' => 'Philippines',
                    'countryiso' => 'PH',
                    'currencyname' => 'Philippino Peso',
                    'code' => '63'
                  },
          'BR' => {
                    'symbol' => 'BRL',
                    'status' => 'LIVE',
                    'countryname' => 'Brazil',
                    'countryiso' => 'BR',
                    'currencyname' => 'Brazilian Real',
                    'code' => '55'
                  },
          'FR' => {
                    'symbol' => 'EUR',
                    'status' => 'offline',
                    'countryname' => 'France',
                    'countryiso' => 'FR',
                    'currencyname' => 'Euro',
                    'code' => '33'
                  }
        };
Run Code Online (Sandbox Code Playgroud)

问题是:为什么散列(countryiso)的键在散列内的值中重复?

我更喜欢的是以下输出:

$VAR1 = {
          'PH' => {
                    'symbol' => 'PHP',
                    'status' => 'LIVE',
                    'countryname' => 'Philippines',
                    'currencyname' => 'Philippino Peso',
                    'code' => '63'
                  },
          'BR' => {
                    'symbol' => 'BRL',
                    'status' => 'LIVE',
                    'countryname' => 'Brazil',
                    'currencyname' => 'Brazilian Real',
                    'code' => '55'
                  },
          'FR' => {
                    'symbol' => 'EUR',
                    'status' => 'offline',
                    'countryname' => 'France',
                    'currencyname' => 'Euro',
                    'code' => '33'
                  }
        };
Run Code Online (Sandbox Code Playgroud)

是否可以使用fetchall_hashref DBI方法?或者我是否必须采用传统方式,循环遍历每一行并动态构建哈希?

Eug*_*ash 3

不,不能使用 来完成fetchall_hashref。但您可以迭代哈希值并删除键:

delete $_->{countryiso} for values %$hash;
Run Code Online (Sandbox Code Playgroud)