Simple DBI Question

Hi,
I’m a Ruby new-bee and am working on a simple DBI
example. I just want to get sysdate back from my Oracle
instance and print it to screen.

It seems like I’m connecting successfully but I don’t
know how to read the result back properly. Below is the
code I’m using - any help appreciated.

Regards,

Doug

equire ‘dbi’

class Dbitest

attr :today_str

DBI.connect(‘DBI:Oracle:fqa1’, ‘my_username’, ‘my_pwd’)
do | dbh |

puts "getting…"
sql = "select to_char(sysdate + 1, ‘MON-DD-YYYY’)
today_str from dual"
dbh.prepare(sql) do | sth |

print “#{ sth.execute()}”

today_str = sth.execute()

end

end

end #end class

d = Dbitest.new
print “today: #{@today_str}”

[courtesy cc of this posting sent to cited author via email]

In article 20021006210639.DKGA17535.rwcrmhc51.attbi.com@rwcrwbc55,

It seems like I’m connecting successfully but I don’t
know how to read the result back properly. Below is the
code I’m using - any help appreciated.

You need to retrieve the data. The method you want are #fetch, #fetch_hash,
and so on.

today_str = sth.execute()

err = sth.execute
@today_str = sth.fetch.to_s
puts row

···

doug.tillman@attbi.com wrote:

end


Ollivier ROBERT -=- FreeBSD: The Power to Serve! -=- roberto@freebsd.org
Usenet Canal Historique

For this kind of SQL query, method select_one of class DatabaseHandle is
probably the best choice.
See the example below:

require ‘dbi’

class Dbitest

attr :today_str

def initialize
DBI.connect(‘DBI:Oracle:fqa1’, ‘my_username’, ‘my_pwd’) do |dbh|
@today_str = dbh.select_one(“select to_char(sysdate + 1,
‘MON-DD-YYYY’) today_str from dual”)
end
end

end # Dbitest

d = Dbitest.new
print “today: #{d.today_str}”

Regards,

Michael

···

On Sun, 2002-10-06 at 23:06, doug.tillman@attbi.com wrote:

Hi,
I’m a Ruby new-bee and am working on a simple DBI
example. I just want to get sysdate back from my Oracle
instance and print it to screen.

It seems like I’m connecting successfully but I don’t
know how to read the result back properly. Below is the
code I’m using - any help appreciated.