Connect to the Oracle database through the OCI8 Interface

Source: Internet
Author: User

There are many ways to connect to the Oracle database. The following describes how to connect to the Oracle database through the OCI8 interface. It is helpful for you to learn how to connect to the Oracle database.

If you have the PL/SQL program, you can perform the following steps smoothly, because oci8 will use the configuration in tnsname to access the database through PL/SQL.

A Use this command to install directly: gem install ruby-oci8
B download file: http://rubyforge.org/projects/ruby-oci8/
B1. download file: ruby-oci8-1.0.6-mswin32.rb
Download to local and run the rb file: ruby ruby-oci8-1.0.6-mswin32.rb
B2. download file: ruby-oci8-1.0.6-x86-mswin32-60.gem
............................N multi-Installation Method
The latest file is as follows:
Ruby-oci8-1.0.6-mswin.rb 56 KB 1,989 i386 Other
Ruby-oci8-1.0.6-x86-mswin32-60.gem 112 KB 780 i386. gem (RubyGem)
Ruby-oci8-1.0.6.gem 116 KB 798 Any. gem (RubyGem)
Ruby-oci8-1.0.6.tar.gz
After the installation is successful, verify whether the connection to the Oracle database is successful. The method is as follows:
# Oci8 must be loaded during use
Require 'ci8'
# Create a connection
Conn = OCI8.new ("username", "password", "oracle Server Name ")

A = []
# Perform query operations
Cursor = conn.exe c ('select * from Table name') {| r | a <r; puts r. join ('')}

Puts r. join ('') is mainly about the output style. You can use puts r. to_s.

Function usage:
1) OCI8.new
OCI8.new (userid, password, dbname = nil, privilege = nil)
Connect to Oracle by userid and password. dbname is the connect string of Net8.
If you need DBA privilege, please set privilege as: SYSDBA or: SYSOPER.
If the Oracle client is 10g or later, you can use "// hostname_or_ip: port_no/oracle_sid" as dbname.

# Sqlplus scott/tiger
Conn = OCI8.new ("scott", "tiger ")

# Sqlplus scott/tiger@orcl.world
Conn = OCI8.new ("scott", "tiger", "orcl. world ")

# Sqlplus 'sys/change_on_install as sysdba'
Conn = OCI8.new ("sys", "change_on_install", nil,: SYSDBA)

# Sqlplus scott/tiger @ // oracle_db.example.com/XE
Conn = OCI8.new ("scott", "tiger", "// oracle_db.example.com/XE ")

2) exec

Exec (SQL, * bindvars)

If you do not configure PL/SQL, and you do not want to use a local tnsname, you can define a function by yourself:

Def open_connection
Tnsnames = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = somehost.somedomain.com) (PORT = 1521) (CONNECT_DATA = (SID = some_sid )))'
Connection = OCI8.new ('your _ username', 'Your _ password', tnsnames)
End

Whether to configure parameters after the open_connection function depends on the project requirements. If your project database is basically one, you can define it directly.

You can also pass parameters.

If the database access address changes a lot, it is necessary to pass the parameter...

Instances that use oracle Stored Procedure Paging

Three common mistakes in Oracle Database Backup

Customize the correct oracle backup policy

Learn about Oracle OS backup

Oracle EXP/IMP backup Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.