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 9F333D700B for ; Sun, 20 Nov 2005 06:31:48 -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 30947-07 for ; Sun, 20 Nov 2005 10:31:47 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from web52901.mail.yahoo.com (web52901.mail.yahoo.com [206.190.49.11]) by svr1.postgresql.org (Postfix) with SMTP id 775E6D6FC3 for ; Sun, 20 Nov 2005 06:31:43 -0400 (AST) Received: (qmail 96547 invoked by uid 60001); 20 Nov 2005 10:31:45 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=hOFkc6SbhtckFIsL65pilSm8ZfpHgZLd05faqLdROcvHicO5BrP8/9na6CFsqvRtrnwNoMGRmJBKXUQgyEDX0Kgs3uNUL5/D1E0oqYhL4yioo+PRKEEeH1xoJeAn2W7ZMOKpKdWIgxeHsfg9YwkHDeRS82M7AB8+nq68XzCVCPU= ; Message-ID: <20051120103145.96545.qmail@web52901.mail.yahoo.com> Received: from [24.8.120.181] by web52901.mail.yahoo.com via HTTP; Sun, 20 Nov 2005 02:31:45 PST Date: Sun, 20 Nov 2005 02:31:45 -0800 (PST) From: CSN Subject: Re: ruby/postgres - getting assoc array of rows? To: Michael Fuhr Cc: pgsql-interfaces@postgresql.org In-Reply-To: <20051120053247.GA31157@winnie.fuhr.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.696 required=5 tests=[DNS_FROM_RFC_ABUSE=0.479, FROM_HAS_ULINE_NUMS=0.217] X-Spam-Score: 0.696 X-Spam-Level: X-Archive-Number: 200511/54 X-Sequence-Number: 5850 I tried: puts "by name: #{row['id']} #{row['name']}" but it exits with: pg.rb:16:in `[]': can't convert String into Integer (TypeError) The by position line does work though. thanks csn --- Michael Fuhr wrote: > 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 > __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com