Forum: Ruby Ruby/MySQL SELECT results

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0665b4b84e7865dac77509d2b26166f?d=identicon&s=25 Paul Willis (paulwillis)
on 2007-03-26 15:19
Hi

I am playing with Ruby/MySQL, I have everything installed ok. I can
INSERT into my database but I am having trouble with SELECT.

I want to see if the text in the field 'title' has been used before and
if it has return the id of that row. If I run...

require "mysql"
my = Mysql::new("hostname", "username", "password", "db")
hasid = my.query("SELECT id FROM table WHERE title = 'Here is a title'")
puts hasid

I get a result like...

#<Mysql::Result:0x55c74c>

I am expecting just the number in the id field. Any ideas?

Paul
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2007-03-26 15:54
(Received via mailing list)
Paul Willis wrote:
> hasid = my.query("SELECT id FROM table WHERE title = 'Here is a title'")
> puts hasid
>
> I get a result like...
>
> #<Mysql::Result:0x55c74c>
>
> I am expecting just the number in the id field. Any ideas?

hasid.each do |row|
   puts row
end

Keep in mind, that Ruby/MySQL doesn't "rewind" after using an iterator.

I've circumvented that by doing the following:

array = Array.new
hasid.each do |row|
   array.push(row)
end


I haven't tested the code, but it mirrors the code I use, sans
implementation details special to my program.

--
Phillip "CynicalRyan" Gawlowski

Rule of Open-Source Programming #9:

Give me refactoring or give me death!
B0665b4b84e7865dac77509d2b26166f?d=identicon&s=25 Paul Willis (paulwillis)
on 2007-03-26 23:10
>> hasid = my.query("SELECT id FROM table WHERE title = 'Here is a title'")
>> puts hasid
>>
>> I get a result like...
>>
>> #<Mysql::Result:0x55c74c>
>>
>> I am expecting just the number in the id field. Any ideas?
>
> hasid.each do |row|
>    puts row
> end
>
> Keep in mind, that Ruby/MySQL doesn't "rewind" after using an iterator.
>
> I've circumvented that by doing the following:
>
> array = Array.new
> hasid.each do |row|
>    array.push(row)
> end

I'm having trouble getting my id out of the array to use as a variable
in an if/then/else statement

I want to get the id (there will only be one in the MySQL database that
matches my query, and then do something like...

if hasid > 0
 then
  puts "already here with id " +hasid
 else
  puts "this will be a new item"
end
Ac0085dae0703db56ad7f8cb9e1798ba?d=identicon&s=25 Phillip Gawlowski (Guest)
on 2007-03-26 23:44
(Received via mailing list)
Paul Willis wrote:

>  else
>   puts "this will be a new item"
> end

Try the Mysql::Result#fetch_field_direct method. This should do what you
want. At least it did when I played with it in irb.

--
Phillip "CynicalRyan" Gawlowski

Rule of Open-Source Programming #6:

The user is always right unless proven otherwise by the developer.
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2007-03-27 22:42
(Received via mailing list)
On Mon, 26 Mar 2007 22:19:04 +0900, Paul Willis wrote:

> hasid =  my.query("SELECT id FROM table WHERE
>
hasid,=my.query("SELECT id FROM table WHERE title = 'Here is a
title'").fetch_row

fetch_row gives you an array with the row in it.

by putting several variables with commas between them, you can unpack
the
array into those variables -- if there's only one element, you can
achieve the same behavior by specifying one variable to assign to,
followed by a comma.

This approach leaves a MySQL::Result object lying around waiting to be
garbage collected before it is closed. This may be wasteful --
personally, I prefer to use DBI instead of MySQL directly, and DBI has a
good way to take care of this cleanup automatically:

require 'dbi'
dbh=DBI.connect('DBI:mysql:db:hostname','username','password')
hasid,=dbh.select_one("SELECT id FROM table WHERE title = 'Here is a
title'")
puts hasid
This topic is locked and can not be replied to.