Re: [SQL] Getting datatype before SELECT - Mailing list pgsql-sql

From darcy@druid.net (D'Arcy J.M. Cain)
Subject Re: [SQL] Getting datatype before SELECT
Date
Msg-id m0zOBj2-00006FC@druid.net
Whole thread Raw
In response to Getting datatype before SELECT  (Glenn Sullivan <glenn.sullivan@nmr.varian.com>)
List 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@{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.

pgsql-sql by date:

Previous
From: David Hartwig
Date:
Subject: Re: [SQL] Getting datatype before SELECT
Next
From: Bob Smither
Date:
Subject: Re: [SQL] Getting datatype before SELECT