Creating table columns from ???

Поиск
Список
Период
Сортировка
Искать

Creating table columns from ???

От:
Chadwick Rolfs <c2304182@webdevel.urban.csuohio.edu>
Дата:
That is, anything but by hand...
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 (  );
  or even better, from perl, or php (I am now using Pg.pm)
    create table form_two ( %hash_with_names_and_values );

How easy does this look?  It seems that this would be though of already,
but the only way I see to make tables is this way...:

create table page_one (
	spacer	text null,
	name	text,
	street	text,
	city	text,
	state	text,
	zip	integer,
	length	integer,
	yorm	text,
	phone	text,
	opt	text,
	fax	text,
	email	text,
	ceo	text,
	senior	text,
	comp	text,
	d_o_c	date,
	webPG	text,
	n_o_b	text,
	pge	integer,
	spacer2	text null
);
(doesn't this seem a bit cumbersome?)

And this is the smallest out of eleven forms (a small web site indeed.)
So what do all those people do with huge sites, and bigger forms?  There
must be something I'm missing.  Please, someone point it out to me, as
this project goes to the funders june 8.
I appreciate everyone's help in all matters, thank you.

Chadwick Rolfs

Re: Creating table columns from ???

От:
Giles Lean <giles@nemeton.com.au>
Дата:

> 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 (  );
>   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
FAQ