Re: Creating table columns from ???

From: Giles Lean <giles(at)nemeton(dot)com(dot)au>
To: Chadwick Rolfs <c2304182(at)webdevel(dot)urban(dot)csuohio(dot)edu>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Creating table columns from ???
Date: 2001-06-02 23:17:05
Message-ID: 3636.991523825@nemeton.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


> It seems that there is a missing function that would be very obvious and
> one of the first things available:
> here's what I want to do:
> create table form_two ( <file with names and values> );
> or even better, from perl, or php (I am now using Pg.pm)
> create table form_two ( %hash_with_names_and_values );
...
> (doesn't this seem a bit cumbersome?)

Well, you have to describe the table once, sometime, but it's source
code like the rest of your application -- no need to retype it:

$ psql < file

If 'file' isn't quite in the 'create table ... ( ... )' syntax you
might need to process it first, but that's easy with a suitable
scripting language, and you mentioned perl already. :-)

In perl (and probably PHP) iterating over a hash building up a string
is also easy, so if you are starting from a perl hash something like
this works:

$query = "create table T (";
for $key (keys %hash) {
$query .= "$comma $key $hash{$key}";
$comma = ",";
}
$query .= ")";

$dbh->do($query);

Hope that helps.

Regards,

Giles

Browse pgsql-novice by date

  From Date Subject
Next Message Nabil Sayegh 2001-06-03 06:36:19 Re: monitoring
Previous Message Mohammed A 2001-06-02 22:21:42 monitoring