Re: PostgreSQL arrays and DBD - Mailing list pgsql-general

From SCassidy@overlandstorage.com
Subject Re: PostgreSQL arrays and DBD
Date
Msg-id OF2E635A33.32B5A740-ON882574A5.0063AF38-882574A5.00640356@overlandstorage.com
Whole thread Raw
In response to PostgreSQL arrays and DBD  ("Александр Чешев" <alex.cheshev@gmail.com>)
List pgsql-general

pgsql-general-owner@postgresql.org wrote on 08/14/2008 01:21:26 AM:

> Hello.
>
> I create a table:
>
> CREATE TABLE groups (
>   group_id serial PRIMARY KEY,
>   name varchar(64) UNIQUE NOT NULL,
>   guests integer[] DEFAULT '{}'
> )
>
> I add a new record to the table:
>
> INSERT INTO groups (name) VALUES ('My friends');
>
> Now the table contains 1 record:
>
> | group_id |    name    | guests
> +----------+------------+--------
> |        1 | My friends | {}
>
> I read the new record from the table using DBI:
>
> my $sth = $dbh->prepare(qq/SELECT * FROM groups/);
> $sth->execute();
> my (@guests, $group);
> push(@guests, $group) while $group = $sth->fetchrow_hashref(); # Line 4
> print $guests[0]->{guests}->[0]; # Why ({group_id=>1, name=>'My friends',
> guests=>[0]}) ?
>
> Output of the script:
>
> Argument "" isn't numeric in null operation at ./guestmanager.pl line 4
> 0
>
> DBD should return a reference to an empty array. But DBD returned
> the reference to the array containing 1 element (0). How can I have
> a different result:
>
> ({group_id=>1, name=>'My friends', guests=>[]})
>
> PS
> Version of DBD::Pg is 2.9.0 .


This can't be the whole program, since you show line 4 (per the error message), which is marked as line 4, but you never connected to the database.
Also, I would put parentheses around the expression in the while, to make sure the precedence is what you think it is.

I have a slightly different DBD::Pg version, so I can't test it exactly.  Does your version support array types?  I don't think mine does (older version).

Susan

----------------------------------------------------
Tiered Data Protection Made Simple
http://www.overlandstorage.com/
----------------------------------------------------

pgsql-general by date:

Previous
From: juliano.freitas@ati.pe.gov.br
Date:
Subject: Experiences with BLOB + PostgreSQL
Next
From: Jeff Davis
Date:
Subject: Re: Custom sort