Legal | Privacy
# bind.rb: How to bind variables
require 'config.rb'

# Create a connection to Oracle
conn = OCI8.new(DB_USER, DB_PASSWORD, DB_SERVER)

# Prepare the data, also display what's hard-coded statement
conn.exec("DELETE FROM test_bind")
conn.exec("INSERT INTO test_bind VALUES(1, 'Unknown')")
conn.exec("INSERT INTO test_bind VALUES(2, 'Unknown')")
conn.exec("INSERT INTO test_bind VALUES(3, 'Unknown')")

# Now update the data using bind variables.
cursor = conn.parse("UPDATE test_bind SET name = :name WHERE id = :id")
cursor.bind_param(1,nil,String,100) #  Or: cursor.bind_param(1, ' '*100)
cursor.bind_param(2,Fixnum)					#  Or: cursor.bind_param(2, id) 

id = 1
['China', 'Korea', 'Japan'].each { |country|
	cursor[1] = country
	cursor[2] = id
	cursor.exec
	id = id + 1
}

# Fetch back the updated data
conn.exec("SELECT * FROM test_bind").fetch do |row|
	puts row.join(',')
end

conn.logoff

puts '-'*80

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy