Connecting to Oracle Db Using Ruby

Connecting to Oracle DB using Ruby

the third parameter needs to be the TNS hostname, if you use SQL plus it is also the third parameter in the connectstring, you can find it also in the tnsnames.ora file in the oracle maps

in SQLPlus : connect user/password@hostname;

in oci8 : conn = OCI8.new('SomeUser','SomePass',hostname)

Here a working sample, obfuscated the parameters of course

require 'oci8'
oci = OCI8.new('****','***','****.***')
oci.exec('select * from table') do |record|
puts record.join(',')
end

How to fetch data from remote oracle database Ruby on rails

Install Oracle Instant Client and Ruby-OCI8

Install ActiveRecord oracle_enhanced adapter (version 1.3.1 is recommended)
gem install activerecord-oracle_enhanced-adapter

Replace the development section in the file config/database.yml with the following content and then save and close the file.

development:
adapter: oracle_enhanced
username: rubyhol
password: welcome
database: localhost/orcl

For more details follow this link: https://www.oracle.com/webfolder/technetwork/tutorials/obe/db/oow10/rubyhol/instructions/rubyrails.htm

How to connect a rails app to a oracle database?

Look at "Ruby on Rails on Oracle: A Simple Tutorial or "How to configure Ruby on Rails with Oracle?".

Ruby on Rails Oracle DB connection

At first you need to install Oracle Instant Client - choose corresponding Linux 32-bit or 64-bit binaries. To be safe use older version 10.2.0.4 which I use in all my Ruby and Oracle projects. Install Basic, SDK and SQL*Plus packages.

Then include Oracle Instant Client installation directory in LD_LIBRARY_PATH environment variable so that Oracle Instant Client dynamic libraries would be in load path.

Then try to install ruby-oci8 gem.

Afterwards install activerecord-oracle_enhanced-adapter gem to be able to access Oracle database from ActiveRecord.

I have posted instructions how to install ruby-oci8 on Mac OS X - majority of this applies to Linux as well (DYLD_LIBRARY_PATH on Mac corresponds to LD_LIBRARY_PATH on Linux).

In addition I have created Sprinkle recipe for automated Oracle client installation on Linux - probably it is not the best way to start with this but in case you want to build automated server installation scripts then you can take a look on it.

Connecting to an Oracle Database from Rails 4

The ruby-OCI8 gem can be configured to make a single SQL query to an Oracle Database, the docs have instructions on how to set up the necessary Oracle Instant Client.



Related Topics



Leave a reply



Submit