http://www.perlmonks.org?node_id=36440

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Attributes------------>up to 30 Attribute Values | | | up to 10
  • Comment on How can I read records from a table in a database and represent it in a hash?
  • Download Code

Replies are listed 'Best First'.
Re: How can I read records from a table in a database and represent it in a hash?
by KM (Priest) on Oct 12, 2000 at 21:58 UTC
    Not exactly sure what you are asking, but taking a peek at Tie::DBI may be useful (or exactly what you want).

    Cheers,
    KM

Re: How can I read records from a table in a database and represent it in a hash?
by cwest (Friar) on Oct 12, 2000 at 21:16 UTC
    while your description makes _no_ sense, I will try to explain.

    Most literally, you can't. Try an array of hash refs:

    my $sth = $dbh->prepare(<<__SQL__); SELECT * FROM $table; __SQL__ $sth->execute; my $table_data = []; push @{$table_data}, $_ while $sth->fetchrow_hashref; $sth->finish;
    On the other hand, if you have an id column ( eg. primary key ), you could easily make it ( or any other column, for that matter ) a key in a Hash of Hashes:
    my $sth = $dbh->prepare(<<__SQL__); SELECT * FROM $table; __SQL__ $sth->execute; my $table_data = {}; $table_data->{$_->{id}} = $_ while $sth->fetchrow_hashref; $sth->finish;
    Enjoy!
    --
    Casey
        I am a Superhero.