Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: selectall_hashref structure

by bobf (Monsignor)
on Dec 24, 2009 at 16:42 UTC ( #814260=note: print w/ replies, xml ) Need Help??


in reply to selectall_hashref structure

If I understand the question (and your subsequent clarification) correctly, I think this should do the trick:

use strict; use warnings; use DBI; use DBD::SQLite; use Data::Dumper; my $dbh = DBI->connect( 'dbi:SQLite:dbname=test.sqlite', undef, undef, { RaiseError => 1, PrintError => 1, } ); eval { $dbh->do('CREATE TABLE evkeypairs (id INTEGER PRIMARY KEY NOT NULL +,key TEXT,value TEXT);'); $dbh->do(q!INSERT INTO evkeypairs (key, value) VALUES ('foo','bar' +);!); $dbh->do(q!INSERT INTO evkeypairs (key, value) VALUES ('kung','foo +');!); } or warn "$@"; my $sth = $dbh->prepare( 'SELECT * FROM evkeypairs' ); $sth->execute; my $href = $sth->fetchall_hashref( 'id' ); print Dumper $href; $dbh->disconnect; unlink 'test.sqlite'; __END__ $VAR1 = { '1' => { 'value' => 'bar', 'id' => '1', 'key' => 'foo' }, '2' => { 'value' => 'foo', 'id' => '2', 'key' => 'kung' } };

Thanks to AM for providing the code to create a test db.


Comment on Re: selectall_hashref structure
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2014-12-25 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls