[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: how to get data from database as string

Brian Candler

6/27/2007 3:04:00 PM

> 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.ruby...

Click on "ruby-oci8 API" on the left-hand side, e.g. to get to
http://ruby-oci8.ruby...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.

4 Answers

Maung Augn

6/27/2007 4:44:00 PM

0

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...., *****, "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 Candler 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.ruby...
>
> Click on "ruby-oci8 API" on the left-hand side, e.g. to get to
> http://ruby-oci8.ruby...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.


--
Posted via http://www.ruby-....

Axel Etzold

6/28/2007 9:07:00 AM

0

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: http://www.gmx.net/de/...

Brian Candler

6/28/2007 9:17:00 AM

0

> 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...., 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...., 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...., row['USERNAME'], row['PASSWORD'])

Brian.

Maung Augn

8/22/2007 7:50:00 PM

0

What about if I want to fetch multiple rows? Any suggestion would be
greatly appreciated.

maung

Brian Candler wrote:
cursor = conn.exec("Select username, password from foo where
> id=1234")
> row = cursor.fetch_hash
> cursor.close
> myLogin1.setup("http://www.testing...., row['USERNAME'],
> row['PASSWORD'])
>
> Brian.
--
Posted via http://www.ruby-....