PostgreSQL arrays and DBD

Started by Alexander Cheshevover 17 years ago2 messagesgeneral
Jump to latest
#1Alexander Cheshev
alex.cheshev@gmail.com

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 .

#2Noname
SCassidy@overlandstorage.com
In reply to: Alexander Cheshev (#1)
Re: PostgreSQL arrays and DBD

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/
----------------------------------------------------