Skip site navigation (1) Skip section navigation (2)

Re: ruby/postgres - getting assoc array of rows?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: CSN <cool_screen_name90001(at)yahoo(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: ruby/postgres - getting assoc array of rows?
Date: 2005-11-20 15:56:14
Message-ID: 20051120155613.GB38669@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-interfaces
On Sun, Nov 20, 2005 at 02:31:45AM -0800, CSN wrote:
> I tried:
> puts "by name:     #{row['id']} #{row['name']}"
> 
> but it exits with:
> pg.rb:16:in `[]': can't convert String into Integer (TypeError)

What versions of Ruby and ruby-postgres are you using?  Did you use
exactly the code I posted or did you do something different?  The
only way to get a PGrow object (other than building it yourself)
is from PGresult#each or some method that calls it, so this won't
work:

conn = PGconn.new('dbname=test')
result = conn.exec('SELECT 1 AS x')
row = result[0]  # returns an Array
puts row['x']    # raises TypeError

but this does work:

row = result.to_a[0]  # returns a PGrow
puts row['x']

I'm not sure if the PGrow behavior is version-specific.  I don't
see it mentioned in the ruby-postgres ChangeLog, so I'd have to dig
a little more to find out.

-- 
Michael Fuhr

In response to

Responses

pgsql-interfaces by date

Next:From: CSNDate: 2005-11-20 19:36:20
Subject: Re: ruby/postgres - getting assoc array of rows?
Previous:From: CSNDate: 2005-11-20 10:31:45
Subject: Re: ruby/postgres - getting assoc array of rows?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group