Thread: Column existence - how to check?

Column existence - how to check?

From
"Mark Higgins"
Date:
I'm writing an app that dynamically creates tables in a postgresql db.

I'd like to determine if a column has been defined for a known table. Is
there any way to do this? I've searched around quite a bit and haven't
found anything.

Re: Column existence - how to check?

From
"Jeff Boes"
Date:
In article <20011002.213141.2025187190.16719@localhost.localdomain>, "Mark
Higgins" <mghiggins@my-deja.com> wrote:

> I'm writing an app that dynamically creates tables in a postgresql db.
>
> I'd like to determine if a column has been defined for a known table. Is
> there any way to do this? I've searched around quite a bit and haven't
> found anything.

Well, if your app is written in Perl, and you are using DBD::Pg, then you
can do something like this:

my $sth = $dbh->prepare('select * from mytable');
$sth->execute or die $dbh->errstr;
my @cols = @{ $sth->{NAME_lc} };
if (grep($_ eq 'col_i_want', @cols)) { ... }

--
Jeff Boes                                             vox 616.226.9550
Database Engineer                                     fax 616.349.9076
Nexcerpt, Inc.                                      jboes@nexcerpt.com