Re: moving from mySQL to pgsql, need a bit of help (perl) - Mailing list pgsql-general

From Ed Loehr
Subject Re: moving from mySQL to pgsql, need a bit of help (perl)
Date
Msg-id 3969F02D.244D9B96@austin.rr.com
Whole thread Raw
In response to moving from mySQL to pgsql, need a bit of help (perl)  (Matt Housh <jaeger@morpheus.net>)
Responses Re: moving from mySQL to pgsql, need a bit of help (perl)
List pgsql-general
Matt Housh wrote:
>
>         In my current code (perl/mySQL), I execute the following query: "SELECT *
> from table_name" - then use the following code to put it in a hash:
>
> $j = 0;
> while (@row = $sth->fetchrow_array)
> {
>    for ($i = 0; $i < $fields; $i++)
>    {
>       $taghash[$j]{$$fieldnames[$i]} = $row[$i];
>    }
>    $j++;
> }
>
>         What I end up with is an array of hashes containing all the table data.
> Each element in the array is a hash called $taghash[$i], and the fields can
> be accessed via $taghash[$i]{'userid'}, $taghash[$i]{'username'}, and
> $taghash[$i]{'userdata'}. $fields and $fieldnames are variables created by a
> subroutine I wrote which is called after the query is executed. $fields is
> simply the number of fields in the table, and $fieldnames is an array
> containing the names of the columns in the table, in order, produced by
> "$fieldnames = $sth->{NAME};" HERE is the problem. The PostgreSQL DBI/DBD
> doesn't seem to support this. I'm looking for something that returns the
> names of the columns in the table, so I can duplicate my code without
> changing too much. Is there anything available to do this? I've yet to find
> it in the documentation, though I may have simply missed it, as I'm new to
> PostgreSQL... Any help is appreciated...

I believe DBI/DBD does this for you:

while ($row_href = $sth->fetchrow_hashref)
{
   push( @taghash, $row_href);
}



Regards,
Ed Loehr

pgsql-general by date:

Previous
From: Matt Housh
Date:
Subject: moving from mySQL to pgsql, need a bit of help (perl)
Next
From: "Robert D. Nelson"
Date:
Subject: RE: PostgreSQL & the BSD License