getting first value of a mysql return ...

D

dc

hi list -

cant find any decent mysql/ruby usage examples, any suggestions? even
the dave thomas book has very little...

i have a sql statement which sometimes returns a single value, and
sometimes it cant find a matching result.

how do i trap this nothing value? it doesnt seem to be a "nil" : the
return type is always:
MySql::Result

which is not a hash, so i cant get the keys or count() or anything...

puts res.class
# Mysql::Result

itemCount = res.keys.length
#> NoMethodError: undefined method 'keys' for #

if ( res.nil? ) // never is tho...

--
also how do i extrac the first and only result? eg if the result is
not a list of items, but just reding the value of one field?

tx!


--
 
R

Robert Klemme

dc said:
hi list -

cant find any decent mysql/ruby usage examples, any suggestions? even
the dave thomas book has very little...

i have a sql statement which sometimes returns a single value, and
sometimes it cant find a matching result.

how do i trap this nothing value? it doesnt seem to be a "nil" : the
return type is always:
MySql::Result

That's the usual way DB interfaces handle this. You always get a result -
but it may be empty. You can either iterate all result rows and notice that
you do not have any result that way. Or you can maybe try #empty? on the
result.

Kind regards

robert
 
A

Andy Shen

Robert said:
That's the usual way DB interfaces handle this. You always get a result -
but it may be empty. You can either iterate all result rows and notice that
you do not have any result that way. Or you can maybe try #empty? on the
result.

Kind regards

robert

Yep, try #num_rows on the result (I had a look at mysql.rb in rails)

Cheers,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top