Re: [SQL] Getting datatype before SELECT

From: darcy(at)druid(dot)net (D'Arcy J(dot)M(dot) Cain)
To: glenn(dot)sullivan(at)nmr(dot)varian(dot)com (Glenn Sullivan)
Cc: pgsql-sql(at)hub(dot)org
Subject: Re: [SQL] Getting datatype before SELECT
Date: 1998-09-30 02:10:44
Message-ID: m0zOBj2-00006FC@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thus spake Glenn Sullivan
> In my C code which communicates with the Postgres database,
> I have the need to determine the datatype of a column, before
> I have done a SELECT command. I have the name of the column,
> but I cannot seem to figure out how to get the datatype information
> until after I have done a SELECT. Then I can call PQfnumber() and
> PQftype() to get the type. Does anyone know how to do this?

Here's a select statement I use to get the types of a class.

SELECT pg_attribute.attname, pg_type.typname
FROM pg_class, pg_attribute, pg_type
WHERE pg_class.relname = '%s' AND
pg_attribute.attnum > 0 AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.atttypid = pg_type.oid

The "%s" gets replaced by the class name and the typname shows the
name of the type. Here's an example output.

attname |typname
---------+-------
ride_id |int4
name |text
from_date|date
to_date |date
contact |text
phone |text
email |text
url |text
(8 rows)

--
D'Arcy J.M. Cain <darcy(at){druid|vex}.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 424 2871 (DoD#0082) (eNTP) | what's for dinner.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bob Smither 1998-09-30 02:30:43 Re: [SQL] Getting datatype before SELECT
Previous Message David Hartwig 1998-09-29 21:28:31 Re: [SQL] Getting datatype before SELECT