开发者

Ruby SQL: undefined method `[]'

I think I'm being stupid.

rc = Mysql.new('CENSORED_HOST','username','password','db')
release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

puts release["release_title"]

rc.close

An开发者_StackOverflow社区d I am getting the following error:

ribcage-connect.rb:17: undefined method `[]' for #<Mysql::Result:0x1011ee900> (NoMethodError)

I'm new to this and I am sure I am basically doing something very stupid. Thanks a lot.

EDIT

The kind people below have got me a but further, now I have:

rc = Mysql.new('server','user','pword','db')

release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')

row = release.fetch_row
puts row['release_title']

rc.close

And now I get the error:

ribcage-connect.rb:12:in `[]': can't convert String into Integer (TypeError)
    from ribcage-connect.rb:12


the rc.query(...) returns a result set you can iterate. You can't directly access it.

Look at:

http://www.kitebird.com/articles/ruby-mysql.html#TOC_7

Since you have only one row (because of LIMIT 1) you can do:

row = release.fetch_hash
puts row['release_title']

If you have multiple rows:

while row = release.fetch_hash do
 puts row["release_title"]
end


That error means you cannot index directly (use []) the Result object.

It should instead be (for this single row case):

row = release.fetch_hash
puts row["release_title"]

For more information: go here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜