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

Hi,
I'm trying to extract not only the data from a table but also the column names.
Is there any function in the DBI module to extract the column names of a table.

Thanks in advance.

use DBI; ... ... ... while ( my @rows = $sth->fetchrow_array ) { print "@rows\n"; }

Replies are listed 'Best First'.
Re: extract the Column names of a table
by wfsp (Abbot) on Mar 24, 2008 at 10:48 UTC
    Take a look at fetchrow_hashref in the DBI docs.
    Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs.

      Thanks for the response.

Re: extract the Column names of a table
by merlyn (Sage) on Mar 24, 2008 at 14:08 UTC
    See the "NAME" and "NAME_lc" attribute of the $sth as described in DBI. For example, after reading any row (or all rows) of your result, you can say:
    my @col_names = @{$sth->{NAME}};
    You might even get the names before the first result—depends on the database.
Re: extract the Column names of a table
by olus (Curate) on Mar 24, 2008 at 12:34 UTC
Re: extract the Column names of a table
by DBAugie (Beadle) on Mar 24, 2008 at 11:40 UTC
    In Oracle, one reveals the column names with
    select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME = 'NAME_OF_TABLE'

    TAMTOWTDI, but it seems to me that using SQL for sql-type things and DBI for database connectivity is a more successful strategy.

    r,

    Augie