X-Original-To: pgsql-interfaces-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9A3ABDBC4B for ; Sun, 20 Nov 2005 01:32:55 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68718-05 for ; Sun, 20 Nov 2005 05:32:54 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id DFDD1DBC29 for ; Sun, 20 Nov 2005 01:32:52 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id jAK5Wm4c052875 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sat, 19 Nov 2005 22:32:51 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id jAK5Wm0N031426; Sat, 19 Nov 2005 22:32:48 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id jAK5WlSY031425; Sat, 19 Nov 2005 22:32:47 -0700 (MST) (envelope-from mfuhr) Date: Sat, 19 Nov 2005 22:32:47 -0700 From: Michael Fuhr To: CSN Cc: pgsql-interfaces@postgresql.org Subject: Re: ruby/postgres - getting assoc array of rows? Message-ID: <20051120053247.GA31157@winnie.fuhr.org> References: <20051120041440.29069.qmail@web52902.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20051120041440.29069.qmail@web52902.mail.yahoo.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[AWL=0.000] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/52 X-Sequence-Number: 5848 On Sat, Nov 19, 2005 at 08:14:40PM -0800, CSN wrote: > Looking at the docs here: > http://ruby.scripting.ca/postgres/reference.html > > there doesn't appear to be an easy way to get an associative row > of rows returns. What exactly are you looking for? The example you posted returns an array of hashes, but depending on what you're doing all that work might not be necessary. PGconn#exec returns a PGresult object, the PGresult#each iterator yields PGrow objects, and PGrow#[] accepts both numeric and text indexes. Example: % psql -d test -c 'SELECT id, name FROM people' id | name ----+------- 1 | Alice (1 row) % cat test.rb require 'postgres' conn = PGconn.new('dbname=test') res = conn.exec('SELECT id, name FROM people') res.each do |row| puts "by name: #{row['id']} #{row['name']}" puts "by position: #{row[0]} #{row[1]}" end res.clear conn.close % ruby test.rb by name: 1 Alice by position: 1 Alice You could also convert the PGresult object into an array of PGrow objects with a one-liner, although you wouldn't get automatic bytea handling as in the function you posted: rows = res.collect Will any of this work for you? If not then please provide more detail. -- Michael Fuhr