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 3FCEBD732F for ; Sun, 20 Nov 2005 15:36:35 -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 21580-05 for ; Sun, 20 Nov 2005 19:36:34 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from web52908.mail.yahoo.com (web52908.mail.yahoo.com [206.190.49.18]) by svr1.postgresql.org (Postfix) with SMTP id 40D87D811E for ; Sun, 20 Nov 2005 15:36:32 -0400 (AST) Received: (qmail 68690 invoked by uid 60001); 20 Nov 2005 19:36:20 -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=6C2NL/JpLZsqKpaiAC7xMthgkrSi7ublrjmIyKaMuYhqlzfL01dKfNcj2Nj6APUOqMMuXcgW+Wf7ebo2wcEafcugSpw7gUS84aNZPcsZHYhIVphFatOM0lqLsYFJVryZyd1902sWxc148Zv0njJ/6WCfhYqu4FyTy0kFhDLOEzo= ; Message-ID: <20051120193620.68688.qmail@web52908.mail.yahoo.com> Received: from [24.8.120.181] by web52908.mail.yahoo.com via HTTP; Sun, 20 Nov 2005 11:36:20 PST Date: Sun, 20 Nov 2005 11:36:20 -0800 (PST) From: CSN Subject: Re: ruby/postgres - getting assoc array of rows? To: Michael Fuhr Cc: pgsql-interfaces@postgresql.org In-Reply-To: <20051120155613.GB38669@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.536 required=5 tests=[AWL=-0.160, DNS_FROM_RFC_ABUSE=0.479, FROM_HAS_ULINE_NUMS=0.217] X-Spam-Score: 0.536 X-Spam-Level: X-Archive-Number: 200511/56 X-Sequence-Number: 5852 --- Michael Fuhr wrote: > 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: I'm using Ruby 1.8 and postgres 0.7.1 (installed via gem). Doing 'gem list -r' I see there's also this, which I don't have installed: postgres-pr (0.4.0, 0.3.6, 0.3.5, 0.3.4, 0.3.3, 0.3.2, 0.3.1, 0.3.0, 0.2.2, 0.2.1, 0.2.0, 0.1.1, 0.1.0, 0.0.1) A pure Ruby interface to the PostgreSQL (>= 7.4) database My code is nearly identical to what you posted, however PGconn.new('dbname=mydb') gives this error: pg.rb:6:in `new': could not translate host name "dbname=mydb" to address: Name or service not known (PGError) This in the only connection code I could get to work: conn = PGconn.connect("localhost", 5432, '', '', "mydb", "user", "password") My code also differs in that I need to put "require 'rubygems'" before "require 'postgres'", otherwise I get this error: `require': no such file to load -- postgres (LoadError) > 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'] With that, I get (I'm doing "select * from items order by id limit 10"): nil nil nil nil nil nil nil undefined method `[]' for nil:NilClass (NoMethodError) Here's my complete code I'm using with various lines comments/uncommented. Using "result.to_a[0]" vs. "result.each", etc. appears to give a different type of row and some of the "puts" lines give errors where they worked before, or work when they previously gave errors. require 'rubygems' require 'postgres' conn = PGconn.connect("localhost", 5432, '', '', "mydb", "user", "password") #conn = PGconn.new("dbname=mydb") #conn = PGconn.connect(:pghost=>"localhost", :dbname=> "mydb", :login=>"user", :password=>"password") res = conn.exec("select * from items order by id limit 10;") #rows=res.collect #for row in res.result res.each do |row| #for row in res.to_a[0] #puts row[0] + row[1] puts "#{row['title']}" # error: undefined method `[]' for nil:NilClass (NoMethodError) #puts "by name: #{row['id']} #{row['title']}" # gives error #print row # works #print row.inspect # works #puts row[0] # works #puts row.getvalue(0,0) # error: undefined method `getvalue' for "7518":String (NoMethodError) end > 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. Thanks for your help! csn > > -- > Michael Fuhr > __________________________________ Start your day with Yahoo! - Make it your home page! http://www.yahoo.com/r/hs