views:

23

answers:

2

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

And 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
A: 

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

Vinko Vrsalovic
Thanks, I was just not reading hard enough.
The Warm Jets
A: 

Hi,

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
sled
Thanks that is it!
The Warm Jets
I not getting this problem - release = rc.query('select * from wp_ribcage_releases where release_id = 1 limit 1')release = release.fetch_rowputs release['release_artist'] = can't convert String into Integer (TypeError) from ribcage-connect.rb:12
The Warm Jets
@The Warm Jets: It's fetch_hash if you want to use column names. fetch_row will use column indexes. Also, you should use a different variable instead of overwriting release, for clarity.
Vinko Vrsalovic
@sled: You have a bug in your first sample, it should be fetch_hash
Vinko Vrsalovic
Got it, thanks again, case closed!
The Warm Jets
Thanks, got it fixed ;)
sled