Use Ruby scripts to operate Oracle databases

Source: Internet
Author: User

This article uses a simple example to illustrate how to write a ruby script to perform Oracle database operations. The main steps are as follows:

1. Download Ruby at the following address:

Http://www.ruby-lang.org/zh_CN/downloads/
Here we download the ruby Windows version:
Click: Ruby 1.8.6 step installation connection, download file: ruby186-26.exe

2. Install Ruby

Double-click ruby186-26.exe to run Ruby.

3. Download and install Ruby/oci8

To enable Ruby to communicate with our Oracle database, Ruby/oci8. Available from
Http://rubyforge.org/projects/ruby-oci8/ download file:

Ruby-oci8-1.0.0-mswin32.rb

Double-click the file to complete Ruby/oci8 installation.

4.write the rubyscript file, read the product ID from the inventory file offeridlist.txt, and change the product
Status task:


Create a text file and enter the followingCode, Save as: update_offer_state.rb

Require 'dbi'

I = 0
DBH = DBI. Connect ('dbi: oci8: tnsdbname', 'username', 'Password ')
Sqlcapitalsupdate = "Update product_offer set state =? Where offer_id in (?) And state =? "

Print "Enter the original product status :"
Old_state = gets
Old_state = old_state.chomp; # Remove the line break after the input line

Print "Enter the product target status :"
Str_state = gets
Str_state = str_state.chomp; # Remove the line break after the input line

Puts ""

File1 = file.open('offeridlist.txt ', "R ")
# STR = file. readlines
# Puts Str
File1.each do | L |
Rs = DBH. Prepare ('select state from product_offer where offer_id = '+ L. to_s)
Rs.exe cute
Rsrow = Rs. Fetch
If rsrow. to_s = old_state.to_s then
Puts L
I = I + 1
DBH. Do (sqlcapitalsupdate, str_state.to_s, L. to_ I, old_state.to_s)
End
End
File1.close
If I. to_ I! = 0 then
Puts ""
Print "and above" + I. to_s + "item statuses have changed from" + old_state.to_s + ":"
Puts str_state
End

DBH. Commit
DBH. Disconnect

Puts ""
Print "press any key to exit :"
Gets
Exit

Create a text file: offeridlist.txt, and save the product ID in the file:

120010020
120010022

Save the files offeridlist.txt and update_offer_state.rb in the same directory.

5. Check the syntax error of the Ruby script.

Enter ruby-CW update_offer_state.rb in the command line to complete the script
Update_offer_state.rb syntax check

If the check syntax is correct, it is shown as follows:

C:/> ruby-CW update_offer_state.rb
Syntax OK

6. Run the ruby script:

(1) Enter Ruby update_offer_state.rb in the command line to start running.
Update_offer_state.rb script

(2) In Windows, double-click the update_offer_state.rb file.
Start running the script

 

Related Article

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.