Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: perl dbi - no column headings

by hardburn (Abbot)
on Jul 09, 2003 at 16:40 UTC ( [id://272736]=note: print w/replies, xml ) Need Help??


in reply to perl dbi - no column headings

TMTOWTDI. This strikes me as an OK time to use SELECT *:

# Use LIMIT 1 to keep the database from working # too hard my $sql = q(SELECT * FROM table LIMIT 1); my $sth = $dbh->prepare($sql) or die . . . ; $sth->execute(); my @col_names = keys %{ $sth->fetchrow_hashref }; $sth->finish();

There are most certainly better ways, though.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: perl dbi - no column headings
by mpeppler (Vicar) on Jul 09, 2003 at 17:12 UTC
    I strongly disagree - you shouldn't run a select * just to get the column names of another query.

    Use the functionality provided by DBI - namely the $sth->{NAME} attribute (or the related NAME_lc and NAME_uc for the lower case and upper case versions) - this will provide column names in the same order as the current query and is completely generic so that it should handle all situations.

    Michael

Re: Re: perl dbi - no column headings
by cchampion (Curate) on Jul 09, 2003 at 16:46 UTC

    This is not the same thing. Using fetchrow_hashref does not handle double column names in the query.

    Please see Re: Re: Field names from DBI? for more details.

      The SQL above isn't doing any JOINs, so I don't see how double column names would be a problem. Now, fetchrow_hashref() has other problems, but collisions aren't one of them in this (simple) case.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

        Your query doesn't. But do you know if the original poster's query has one? I don't, and when in doubt I advise for safety.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-20 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found