Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: selectall_arrayref with headers

by Anonymous Monk
on May 16, 2012 at 19:54 UTC ( [id://970909]=note: print w/replies, xml ) Need Help??


in reply to selectall_arrayref with headers

Get headers first , see table_info in DBI, then use arrayref

See also ORLite

Replies are listed 'Best First'.
Re^2: selectall_arrayref with headers
by eddor1614 (Beadle) on May 16, 2012 at 20:23 UTC
    The problem is that the select statements are somewhat complex, and I'm trying to use the same array iteration for different SELECT statements.
    An example:

    my $dbfile = "/tmp/storagedb.sqlite"; my $db; my $rep = (); my $selectstring = shift; # Subs $db = DBI->connect("dbi:SQLite:$dbfile","","") or die "ERROR: $!"; $rep = $db->selectall_arrayref($selectstring); if (@$rep) { foreach my $i (@$rep) { foreach my $j (@$i) { print "$j | "; } print $/; } } $db->disconnect;

    I need to put headers before the data, but the $selectstring may have different columns.

      The Slice => {} option retrieves all column names for each row, as it returns hashes. You can then use these as the keys.

      An (somewhat ugly) alternative is to add WHERE 1 = 0 to the query and run it and afterwards ask the statement handle for the names of its columns. This has the advantage of keeping all columns in the same order as the select clause, but has the disadvantage of needing to shim the clause into an existing string.

      DBI has more on the topic.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 15:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found