469,330 Members | 1,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

how do i get field names of a table in postgresql?

Hi all,

I need to get the field names of a table in postgresql?

Like if a table called "employee" which has several fields like 'name','id','address','phone','workexperience'..et c..

I need to fetch the field names and print that into my output..

I have used the query called "show by column from employees"

Its not working...can some body help me to solve this problem.

thanks in advance...

Barani
Aug 13 '07 #1
4 3294
miller
1,089 Expert 1GB
Typically, the query one uses to obtain information about a SQL based table is "DESC employee".

Expand|Select|Wrap|Line Numbers
  1. my $sth = $dbh->prepare(q{DESC employee});
  2. $sth->execute or die $dbh->errstr;
  3. while (my $hashref = $sth->fetchrow_hashref) {
  4.     print $hashref->{Field}, "\n";
  5. }
  6. $sth->finish; undef $sth;
  7.  
-Miller
Aug 13 '07 #2
hi miller,
thanks for your help...Here is the code i have tried with your help...

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI;
  3. use DBI;
  4.  
  5. $sth="dbi:Pg:dbname=barani";
  6. $dbh=DBI->connect("$sth","postgres","india")or die "can't connect$DBI::errstr\n\n";
  7.  
  8.  
  9. $sth = $dbh->prepare(q{DESC employee});
  10. $sth->execute or die $dbh->errstr;
  11. while ($hashref = $sth->fetchrow_hashref) {
  12.     print $hashref->{Field}, "\n";
  13. }
  14. $sth->finish; undef $sth;
  15.  
But i am getting the output as
"DBD::Pg::st execute failed: ERROR: parser: parse error at or near "DESC" at character 1 at empp.cgi line 10.
ERROR: parser: parse error at or near "DESC" at character 1 at empp.cgi line 10."

Please let me know how to solve this issue..

Barani
Aug 14 '07 #3
miller
1,089 Expert 1GB
You're going to have to ask the people in the PostgreSQL Forum what the proper query is for inspecting a tables format. It appears that "DESC table" doesn't work, but maybe they know an alternative.

- Miller
Aug 14 '07 #4
Thanks alot for your guidance Mr .miller..

Hope i will get more help from you..
Aug 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Thierry B. | last post: by
4 posts views Thread by Josué Maldonado | last post: by
1 post views Thread by Don Leverton | last post: by
1 post views Thread by Wei Wang | last post: by
3 posts views Thread by Justin Clift | last post: by
2 posts views Thread by Tim Vadnais | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.