Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

selectall_arrayref with headers

by eddor1614 (Beadle)
on May 16, 2012 at 19:50 UTC ( #970904=perlquestion: print w/replies, xml ) Need Help??
eddor1614 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody.

Anyone knows if it's possible to get column headers with selectall_arrayref using DBI and SQLite?.
Like when you do it from the command line:

sqlite> .headers on
sqlite> select 1 as One,2 as Two;

I know I can get that using selectall_hashref, but I need an array, I want an array !

Replies are listed 'Best First'.
Re: selectall_arrayref with headers
by runrig (Abbot) on May 16, 2012 at 21:35 UTC
    prepare, execute, get headers with $sth->{NAME} (or NAME_uc or NAME_lc), then fetchall_arrayref.
      that was it. thanks!
Re: selectall_arrayref with headers
by Anonymous Monk on May 16, 2012 at 19:54 UTC

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

    See also ORLite

      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970904]
Approved by ww
[Corion]: But if you are parsing CSV, again, let me point to Text::CSV_XS, which you should use instead
[hippo]: +1 for Text::CSV_XS
[ovedpo15]: Im not allowed to use additional modules, so I can't use it. looking for a simple regex to do this task though..
[Corion]: Ah, so this is homework? Then most likely, you should review your course material closer. It would greatly surprise me if the course material wouldn't mention the topics that will be used in the homework...
[Corion]: I already showed you the simple regex to match the last part of a string without a comma in it
[Tux]: Corion++ hippo++
[ovedpo15]: it isn't homework. its a module I use at work. The process to add additional module isn't worth it. p.s never heard a university course which teach perl
[marto]: there are many, to this day.
[ovedpo15]: @Corion Yes I use the regex you showed me. its very good regex. although now i need to know that substring unit that comma
marto mad corrections to one around 18 months ago

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-05-27 10:41 GMT
Find Nodes?
    Voting Booth?