X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id ED23C5E46E9 for ; Wed, 1 Sep 2004 13:28:10 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43829-09 for ; Wed, 1 Sep 2004 16:28:16 +0000 (GMT) Received: from window.monsterlabs.com (window.monsterlabs.com [216.183.105.176]) by svr1.postgresql.org (Postfix) with SMTP id 387C45E46C3 for ; Wed, 1 Sep 2004 13:28:07 -0300 (ADT) Received: (qmail 8600 invoked from network); 1 Sep 2004 16:28:14 -0000 Received: from pcp09180718pcs.nash01.tn.comcast.net (HELO ?192.168.0.106?) (68.53.156.173) by 0 with SMTP; 1 Sep 2004 16:28:14 -0000 In-Reply-To: <200409011714.49671.erik.wasser@iquer.net> References: <200409011714.49671.erik.wasser@iquer.net> Mime-Version: 1.0 (Apple Message framework v619) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: pgsql-sql@postgresql.org From: Thomas F.O'Connell Subject: Re: Extracting fieldnames from a TABLE Date: Wed, 1 Sep 2004 11:28:12 -0500 To: Erik Wasser X-Mailer: Apple Mail (2.619) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/21 X-Sequence-Number: 18867 One way to do this is to use the column_info database handle method. Here's a little perl script that accepts a table name as an argument and returns the column names: #!/usr/bin/perl use DBI; use strict; my( $database, $table ) = @ARGV; my $dbh = DBI->connect( "dbi:Pg:dbname=$database", 'postgres' ); my $sth = $dbh->column_info( undef, 'public', $table, '%' ); $sth->execute; while( my @row = $sth->fetchrow_array ) { print join( ' ', $row[ 3 ] ), "\n"; } $sth->finish; $dbh->disconnect; This could be easily modified to stick the contents of $row[ 3 ] into an array. You'd have to modify the user and schema as appropriate for your database. The fourth parameter to column_info is a wildcard so you get everything. -tfo On Sep 1, 2004, at 10:14 AM, Erik Wasser wrote: > Hi community, > > I would like to retrieve all the fieldnames of a given table. In the > perl module Tie::DBI[1] i found the following fragment: > > $dbh->prepare("LISTFIELDS $table"); > > in the case the DB supports this (Tie::DBI thinks so for Pg) or the > alternative is: > > $dbh->prepare("SELECT * FROM $table WHERE 0=1"); > > The first one doesn't work in my PostgreSQL 7.4.3: > > % LISTFIELDS foobar; > ERROR: syntax error at or near "LISTFIELDS" at character 1 > % > > and the seconds one looks ugly. Is there a solution for the problem? > > Greetings > > [1]http://search.cpan.org/~lds/Tie-DBI-0.93/lib/Tie/DBI.pm > > -- > So long... Fuzz