How can i read a cell from database and retrieve as a string in ruby
with OCI8?
There are working examples in the documentation at
http://ruby-oci8.rubyforge.org/
Click on “ruby-oci8 API” on the left-hand side, e.g. to get to
http://ruby-oci8.rubyforge.org/en/api_OCI8Cursor.html#l9
Basically you need to call fetch or fetch_hash on your cursor to get the
results from the select statement.
Alternatively, if you call exec() directly on the OCI8 object, you can
pass
a block. The one-liner example on the front page demonstrates this:
OCI8.new(‘scott’, ‘tiger’).exec(‘select * from emp’) do |r| puts
r.join(‘,’); end
HTH,
Brian.
Thank you for your response, Brian. Actually what i am trying to do is
that use the string value I get from DB as an argument in another
statement. eg:
conn = OCI8.new('user', 'pwd', 'testingDB.com')
cursor=conn.exec("Select name
From empl
Where ssn=123456789)
while r = cursor.fetch()
r
end
cursor.close
conn.logoff
myLogin1 = Login.new
myLogin1.setup("http://www.testing.com/", *****, "pwd")
***** this is where i want to put the string value from DB. That’s
supposed to be a user name. How could i do that? I have spent so much
time on it. Could anyone help?
Thanks,
Maung
Brian C. wrote:
How can i read a cell from database and retrieve as a string in ruby
with OCI8?
There are working examples in the documentation at
http://ruby-oci8.rubyforge.org/
Click on “ruby-oci8 API” on the left-hand side, e.g. to get to
http://ruby-oci8.rubyforge.org/en/api_OCI8Cursor.html#l9
Basically you need to call fetch or fetch_hash on your cursor to get the
results from the select statement.
Alternatively, if you call exec() directly on the OCI8 object, you can
pass
a block. The one-liner example on the front page demonstrates this:
OCI8.new(‘scott’, ‘tiger’).exec(‘select * from emp’) do |r| puts
r.join(‘,’); end
HTH,
Brian.
Thank you for your response, Brian. Actually what i am trying to do is
that use the string value I get from DB as an argument in another
statement. eg:
conn = OCI8.new('user', 'pwd', 'testingDB.com')
cursor=conn.exec("Select name
From empl
Where ssn=123456789")
r = cursor.fetch # fetch just one row
cursor.close
conn.logoff
myLogin1 = Login.new
myLogin1.setup("http://www.testing.com/", r[0], "pwd")
That’s one solution. Useful for debugging is ‘puts r.inspect’, or ‘p r’
for
short, to see what r actually contains.
Or you might write:
cursor = conn.exec("Select username, password from foo where
id=1234")
username, password = *cursor.fetch
cursor.close
myLogin1.setup(“http://www.testing.com/”, username, password)
Or:
cursor = conn.exec("Select username, password from foo where
id=1234")
row = cursor.fetch_hash
cursor.close
myLogin1.setup(“http://www.testing.com/”, row[‘USERNAME’],
row[‘PASSWORD’])
Brian.
What about if I want to fetch multiple rows? Any suggestion would be
greatly appreciated.
maung
Brian C. wrote:
cursor = conn.exec("Select username, password from foo where
id=1234")
row = cursor.fetch_hash
cursor.close
myLogin1.setup(“http://www.testing.com/”, row[‘USERNAME’],
row[‘PASSWORD’])
Brian.
Dear Maung,
I haven’t worked with OCI8, so this is just general
guessing ;).
Can’t you write a method that will extract the username
like this:
class OCI8
def initialize(user,some_other_data)
@user=user
# …
@passwd=some_other_data
end
def return_user
@user
end
end
i_my_self=OCI8.new(‘axel’,‘abracadabra’)
p i_my_self.return_user => ‘axel’ ,
you could then use that method to retrieve the username
anywhere you need it.
I think this should be available somewhere already,
so if you try:
p i_my_self.methods.sort
you might find it.
Best regards,
Axel
def
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.