This part heavily references a tutorial provided by Oracle.
Part 2: My-first-app with Oracle
This guide assumes you are either:
- connecting to an Oracle Database installed on your local machine.
- connecting to a remote database using Oracle Instant Client installed on your local machine.
Install Oracle Database Driver
Download the driver and install with:
ruby ruby-oci8-1.0.0-rc3-mswin32.rb
You can test your connection to the database with the following one-liner:
ruby -r oci8 -e "OCI8.new('username', 'password', '127.0.0.1:1521/orcl').exec('SELECT * FROM test_table') do |r| puts r.join('|'); end"
Of course, make sure the schema & table that you are testing exists!
Create a User
Using SQL*Plus, create a user with DBA privileges that you can use for this application.
ruby DEFAULT TABLESPACE users TEMPORARY TABLESPACE temp;
EXIT dba TO ruby IDENTIFIED BY ruby;
Create a Table
id NUMBER(10) NOT NULL,
title VARCHAR2(60),
issue NUMBER(4),
publisher VARCHAR2(60),
PRIMARY KEY (id)
);
; (
Alternatively use this SQL file to create the application table, COMICS.
sqlplus ruby/ruby@rails @comics.sql
Note, in this case the database SID is ‘rails’. The default SID for Oracle Enterprise Edition 10g is ‘orcl’, for Express Edition it’s ‘XE’
Create a dinky Application
Your application skeleton:
rails comics_catalog
cd comics_catalog
Edit ‘comics_catalog/config/databases.yml’ – rails needs to know your login & password info.
Within your project directory, there is a directory called config and in it is a file named database.yml. You need to edit database.yml using your favorite text editor. Initially, the file will look like this:
adapter: mysql
database: rails_development
host: localhost
username: root
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
adapter: mysql
database: rails_test
host: localhost
username: root
adapter: mysql
database: rails_production
host: localhost
username: root
Change the development properties as follows:
adapter: oci
username: ruby
password: ruby
host: 127.0.0.1:1234/orcl
host: takes a connect string of the form <ip_address>:<port>/<database> for the machine that your Oracle instance resides on.
Use the magical scaffold command to build a web based CRUD interface to the table we created above...
ruby script/generate scaffold Comic
Now you can access your Rails Comic Catalog application on your own development machine, using your favorite Web browser. Just access the following URL: http://localhost:3000/comics/list
Phew! Done. Last step is Part 3: Pen of pesky Mongrels...
5 comments:
Granting DBA access is not advised. Does this advice come from an old Oracle article? Only the necessary privileges (CREATE TABLE, CREATE SEQUENCE) etc should be used.
Thanks for that! Yes, as mentioned above the advice comes from here. I hope that the guide will evolve as I receive more comments (read: corrections) from others.
Really nice blog post. provided a helpful information. I hope that you will post more updates like this Ruby on Rails Online Training
Data Engineer has duties and responsibilities, we are going to cover Microsoft Azure Data Engineer Associate DP-203 Interview Questions that give you an idea and understanding.
Post a Comment