Ruby dbi - problem with select_one()

Hello, I’m trying to learn the ruby dbi and having some trouble with the
select_one function. Could someone please tell me what I’m doing wrong?

In the following snippet, the block never gets executed:

dbh.select_one(“SELECT login, password FROM user WHERE userid = 1”) do |row|
vars[‘login’] = row[0]
vars[‘password’] = row[1]
end

even though I know it returns a row. If I switch it to use select_all, it
works fine.

Thanks in advance for your help.

Carl Youngblood

Hello, I’m trying to learn the ruby dbi and having some trouble with the
select_one function. Could someone please tell me what I’m doing wrong?

In the following snippet, the block never gets executed:

dbh.select_one(“SELECT login, password FROM user WHERE userid = 1”) do |row|
vars[‘login’] = row[0]
vars[‘password’] = row[1]
end

select_one returns only one row object and is not callable with block.
Your example should be:

row = dbh.select_one(“SELECT login, password FROM user WHERE userid = 1”)
vars[‘login’] = row[0]
vars[‘password’] = row[1]

In Ruby/DBI blocks are either used for iteration (e.g. select_all, fetch) or
for controlled resource destruction (e.g. prepare, execute, connect).

even though I know it returns a row. If I switch it to use select_all, it
works fine.

Yes, it returns a row, but it do not passes it to a block.

select_all is for SELECT statements which return more than one row.
Use select_one if you are sure the statement returns exactly one
row, or you only want to fetch the first row of it (then make sure you
have an ORDER BY clause).

Thanks in advance for your help.

Regards,

Michael

···

On Sat, Jul 13, 2002 at 04:38:35AM +0900, Carl Youngblood wrote: