Ruby-oci8 is the Oracle connector for Ruby, using this library your Ruby programs can communicate with the Oracle database.
If you have not installed Ruby yet on your machine, please refer to the Ubuntu package on this post.
Install few additional Linux packages:
sudo apt-get install libaio-dev unzip
Create an Oracle folder:
sudo mkdir /opt/oracle
Go the Oracle website to download the Instant Client Basic and SDK for Linux 64 bits.
Download files in the Oracle folder you have created and unzip them:
cd /opt/oracle
unzip oracle-basic-11.zip
unzip oracle-sdk-11.zip
Once you have unzip the 2 zip files, go in the Instant Client folder and create a link for application that does not know Instant Client 11.
cd instantclient_11_2/
sudo ln -s libclntsh.so.11.1 libclntsh.so
Create the Oracle Instant Client system variable
export LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2
Then, install ruby-oci8:
sudo env LD_LIBRARY_PATH=/opt/oracle/instantclient_11_2 /usr/bin/gem install ruby-oci8
Try to connect to the database, create a Ruby file for example sql.rb and copy/paste the following code:
require ‘rubygems’
require ‘oci8′
tnsnames = ‘(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = host_name_or_ip)(PORT = 1521)) (CONNECT_DATA = (SID = SID)))’
conn = OCI8.new(‘user’, ‘password’, tnsnames)
cursor = conn.exec(‘SELECT sysdate FROM dual’)
while r = cursor.fetch()
puts r.join(‘,’)
end
cursor.close
conn.logoff
And run it:
ruby sql.rb
Fri Sep 24 08:28:00 +0900 2010
Enjoy and have fun with Ruby and Oracle.
Error messages:
sql:1:in `require’: no such file to load — oci8 (LoadError)
you forgot to put the below line before require ‘oci8′
require ‘rubygems’
or
you can also create an system variable that will automatically use rubygems:
RUBYOPT=”rubygems”
export RUBYOPT
/var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so: libclntsh.so.11.1: cannot open shared object file: No such file or directory – /var/lib/gems/1.8/gems/ruby-oci8-2.0.4/lib/oci8lib_18.so (LoadError)
The LD_LIBRARY_PATH variable is not well configured.
This was a actually very good post. In theory I’d like to create like this also – getting time and genuine work to produce a good article… but what can I say… I procrastinate alot and never appear to get something accomplished.
Lastly a good blog post related to the topic, continue the great work and I hope to learn even more of your stuff in the long run.
Hi thank you for the script. Good work, this really did help me.
Thanks-a-plenty, I managed to install the gem following the steps you describe
Finally something I was looking for. I mean I view like 10 sites (some of them looking really posh) but I found only here what I was looking for
.
Thank you on your help!
really…
Fat women and girls always worried because of their ugly shaped bodies. They want to wear expensive and stylish outfits . http://bdczxnj.allmyblog.com/ but they can not wear these outwears…
Greate…
It’s such a great site! http://www.zimbio.com/Bollywood+Movies/articles/6el_w1DbA40/IIFA+awards+start+in+Toronto Great post, I just bookmarked it on Digg….