diff --git a/ruby/CRUD-examples/README.md b/ruby/CRUD-examples/README.md new file mode 100644 index 00000000..8155ac50 --- /dev/null +++ b/ruby/CRUD-examples/README.md @@ -0,0 +1 @@ +The scripts in this directory follow the [BASIC CRUD OPERATIONS USING RUBY-OCI8](https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/) tutorial found on [LearnCodeShare.net](https://learncodeshare.net). diff --git a/ruby/CRUD-examples/createDBObjects.sql b/ruby/CRUD-examples/createDBObjects.sql new file mode 100644 index 00000000..7de64a26 --- /dev/null +++ b/ruby/CRUD-examples/createDBObjects.sql @@ -0,0 +1,49 @@ +# The following creates and populates the tables used for the tutorial +# These scripts use identity columns available in Oracle 12.1. If you are using an older version you will need +# to add your own trigger/sequence functionality to generate the id values. +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ + +CREATE TABLE LCS_PEOPLE ( + id NUMBER GENERATED BY DEFAULT AS identity, + name VARCHAR2(20), + age NUMBER, + notes VARCHAR2(100) +) +/ + +ALTER TABLE LCS_PEOPLE +ADD CONSTRAINT PK_LCS_PEOPLE PRIMARY KEY ("ID") +/ + +CREATE TABLE LCS_PETS ( + id NUMBER GENERATED BY DEFAULT AS IDENTITY, + name VARCHAR2(20), + owner NUMBER, + type VARCHAR2(100) +) +/ + +ALTER TABLE LCS_PETS ADD CONSTRAINT PK_LCS_PETS PRIMARY KEY ("ID") +/ + +ALTER TABLE LCS_PETS ADD CONSTRAINT FK_LCS_PETS_OWNER FOREIGN KEY ("OWNER") REFERENCES "LCS_PEOPLE" ("ID") +/ + +INSERT INTO LCS_PEOPLE (name, age, notes) + VALUES ('Bob', 35, 'I like dogs') +/ + +INSERT INTO LCS_PEOPLE (name, age, notes) + VALUES ('Kim', 27, 'I like birds') +/ + +INSERT INTO LCS_PETS (name, owner, type) + VALUES ('Duke', 1, 'dog') +/ + +INSERT INTO LCS_PETS (name, owner, type) + VALUES ('Pepe', 2, 'bird') +/ + +COMMIT +/ diff --git a/ruby/CRUD-examples/delete/extra_fun_1.rb b/ruby/CRUD-examples/delete/extra_fun_1.rb new file mode 100644 index 00000000..f2ffffd5 --- /dev/null +++ b/ruby/CRUD-examples/delete/extra_fun_1.rb @@ -0,0 +1,44 @@ +# The example code below executes a simple delete using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Example code showing a simple delete using named bind variables. +statement = 'delete from lcs_pets where type = :type' +cursor = con.parse(statement) +cursor.bind_param(:type, 'bird') +cursor.exec +con.commit +# End Example + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/extra_fun_2.rb b/ruby/CRUD-examples/delete/extra_fun_2.rb new file mode 100644 index 00000000..74e0f220 --- /dev/null +++ b/ruby/CRUD-examples/delete/extra_fun_2.rb @@ -0,0 +1,52 @@ +# The example code below executes two simple deletes using named bind variables. +# The child records are removed, followed by the parent record. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +# Example code showing two simple deletes using named bind variables. +statement = 'delete from lcs_pets where owner = :owner' +cursor = con.parse(statement) +cursor.bind_param(:owner, 5) +cursor.exec + +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 5) +cursor.exec +con.commit +# End Example + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/foreign_keys.rb b/ruby/CRUD-examples/delete/foreign_keys.rb new file mode 100644 index 00000000..fe8b8844 --- /dev/null +++ b/ruby/CRUD-examples/delete/foreign_keys.rb @@ -0,0 +1,49 @@ +# The example code below executes a simple delete using named bind variables. +# When following the tutorial with default data this section intentionally throws an error +# to demonstrate foreign key functionality. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Deleting records referenced by Foreign Keys" 1st example + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +# Example code showing a simple delete using named bind variables. +# The FK will cause this to throw an error. +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit +# End Example + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/handle_pets.rb b/ruby/CRUD-examples/delete/handle_pets.rb new file mode 100644 index 00000000..7ae09c31 --- /dev/null +++ b/ruby/CRUD-examples/delete/handle_pets.rb @@ -0,0 +1,54 @@ +# The example code below executes two simple deletes using named bind variables. +# The child records are updated to a different parrent, +# followed by deleting the original parent record. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Deleting records referenced by Foreign Keys" 2nd example + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original People Data', 'people') +get_all_rows('Original Pet Data', 'pets') + +# Example code showing how to update existing child records before deleting the parent. +statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:oldOwner, 1) +cursor.exec + +statement = 'delete from lcs_people where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit +# End Example + +get_all_rows('New People Data', 'people') +get_all_rows('New Pet Data', 'pets') diff --git a/ruby/CRUD-examples/delete/reset_data.rb b/ruby/CRUD-examples/delete/reset_data.rb new file mode 100644 index 00000000..951d1daf --- /dev/null +++ b/ruby/CRUD-examples/delete/reset_data.rb @@ -0,0 +1,49 @@ +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Resetting the data" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Bob Kim Cheryl Bob Stacey Pete Pat]) +cursor.bind_param_array(:age, [35, 27, 23, 27, 45, 23, 36]) +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds', 'I like horses', 'I like rabbits', 'I like snakes', 'I like cats', 'I like dogs']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Duke Dragon Sneaky Red Red Buster Fido]) +cursor.bind_param_array(:owner, [1, 2, 5, 2, 3, 1, 7]) +cursor.bind_param_array(:type, %w[dog bird snake bird horse dog cat]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/delete/simple.rb b/ruby/CRUD-examples/delete/simple.rb new file mode 100644 index 00000000..7995cc62 --- /dev/null +++ b/ruby/CRUD-examples/delete/simple.rb @@ -0,0 +1,44 @@ +# The example code below executes a simple delete using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Simple delete" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Example code showing a simple delete using named bind variables. +statement = 'delete from lcs_pets where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:id, 1) +cursor.exec +con.commit +# End Example + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/delete/template.rb b/ruby/CRUD-examples/delete/template.rb new file mode 100644 index 00000000..ffb5fa62 --- /dev/null +++ b/ruby/CRUD-examples/delete/template.rb @@ -0,0 +1,39 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/09/delete-crud-using-ruby-oci8/ +# section titled "Boilerplate template" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Your code here + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/dropDBObjects.sql b/ruby/CRUD-examples/dropDBObjects.sql new file mode 100644 index 00000000..53fec410 --- /dev/null +++ b/ruby/CRUD-examples/dropDBObjects.sql @@ -0,0 +1,8 @@ +# The following drops the tables created for the tutorial +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ + +drop table LCS_PETS +/ + +drop table LCS_PEOPLE +/ diff --git a/ruby/CRUD-examples/insert/extra_fun_1.rb b/ruby/CRUD-examples/insert/extra_fun_1.rb new file mode 100644 index 00000000..c0da8ddd --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_1.rb @@ -0,0 +1,43 @@ +# The example code below executes a simple insert using named bind variables. +# The same statement is executed twice each using different bind variable values. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 1 & 2" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing how to insert multiple rows with multiple database calls. +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Rob') +cursor.bind_param(:age, 37) +cursor.bind_param(:notes, 'I like snakes') +cursor.exec + +cursor.bind_param(:name, 'Cheryl') +cursor.bind_param(:age, 41) +cursor.bind_param(:notes, 'I like monkeys') +cursor.exec +con.commit +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/extra_fun_2.rb b/ruby/CRUD-examples/insert/extra_fun_2.rb new file mode 100644 index 00000000..ef01b0aa --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_2.rb @@ -0,0 +1,43 @@ +# The example code below executes a simple insert using named bind variables. +# The get_all_rows function is modified to use a second connection to show how the data is seen +# by different connections before and after a commit. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 1 & 2" + +require 'oci8' + +def get_all_rows(label, con) + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) +con2 = OCI8.new(connectString) + +get_all_rows('Original Data', con) + +# Example code showing how to insert multiple rows with a single database call. +# Extra calls to get_all_rows will demonstrate the state of the data before a commit. +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Suzy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like rabbits') +cursor.exec + +get_all_rows('New connection after insert', con2) +get_all_rows('Same connection', con) + +con.commit +# End Example + +get_all_rows('New connection after commit', con2) +get_all_rows('New Data', con) diff --git a/ruby/CRUD-examples/insert/extra_fun_3.rb b/ruby/CRUD-examples/insert/extra_fun_3.rb new file mode 100644 index 00000000..728265aa --- /dev/null +++ b/ruby/CRUD-examples/insert/extra_fun_3.rb @@ -0,0 +1,46 @@ +# The example code below executes a simple insert using named bind variables. +# Cursor variables are used to accept the insert statement's returning values. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Extra Fun 3" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing how to work with returning vales from the insert. +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id, name into :id, :name_out' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.bind_param(:id, Integer) +cursor.bind_param(:name_out, String) +cursor.exec + +new_id = cursor[:id] +name_out = cursor[:name_out] + +con.commit + +printf " Our new id is: %d name: %s\n\n", new_id, name_out +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/multiple.rb b/ruby/CRUD-examples/insert/multiple.rb new file mode 100644 index 00000000..c70727e8 --- /dev/null +++ b/ruby/CRUD-examples/insert/multiple.rb @@ -0,0 +1,39 @@ +# The example code below executes an insert using exec_array to create multiple rows with a single call. +# +# More details can be found in the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Insert more than 1 row" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing how to insert multiple rows with a single database call. +cursor = con.parse('INSERT INTO lcs_people(name, age, notes) VALUES (:name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:name, %w[Sandy Suzy]) +cursor.bind_param_array(:age, [31, 29]) +cursor.bind_param_array(:notes, ['I like horses', 'I like rabbits']) +people_row_count = cursor.exec_array +con.commit +printf " Successfully inserted %d records\n\n", people_row_count +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/reset_data.rb b/ruby/CRUD-examples/insert/reset_data.rb new file mode 100644 index 00000000..4c50210a --- /dev/null +++ b/ruby/CRUD-examples/insert/reset_data.rb @@ -0,0 +1,50 @@ +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Resetting the data" + +# Query all rows +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Bob Kim]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Duke Pepe]) +cursor.bind_param_array(:owner, [1, 2]) +cursor.bind_param_array(:type, %w[dog bird]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/insert/return_data.rb b/ruby/CRUD-examples/insert/return_data.rb new file mode 100644 index 00000000..b5a7b454 --- /dev/null +++ b/ruby/CRUD-examples/insert/return_data.rb @@ -0,0 +1,62 @@ +# The example code below executes a simple insert using named bind variables. +# A cursor variable is used to accept the insert statements returning value. This value is then +# used as the parent key value to insert a child record. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Returning data after an insert" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing how to work with returning vales from the insert. +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes) returning id into :id' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.bind_param(:id, Integer) +cursor.exec + +new_id = cursor[:id] + +statement = 'insert into lcs_pets (name, owner, type) values (:name, :owner, :type)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Big Red') +cursor.bind_param(:owner, new_id) +cursor.bind_param(:type, 'horse') +cursor.exec + +con.commit + +printf " Our new value is: %d\n", new_id + +statement = 'select name, owner, type from lcs_pets where owner = :owner' +cursor = con.parse(statement) +cursor.bind_param(:owner, new_id) +cursor.exec +printf "\n Sandy\'s pets:\n" +cursor.fetch do |row| + printf " Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2] +end +printf "\n" +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/simple.rb b/ruby/CRUD-examples/insert/simple.rb new file mode 100644 index 00000000..c48b6168 --- /dev/null +++ b/ruby/CRUD-examples/insert/simple.rb @@ -0,0 +1,37 @@ +# The example code below executes a simple insert using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/10/04/insert-crud-using-ruby-oci8/ +# section titled "Simple insert" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing a simple insert using named bind variables. +statement = 'insert into lcs_people(name, age, notes) values (:name, :age, :notes)' +cursor = con.parse(statement) +cursor.bind_param(:name, 'Sandy') +cursor.bind_param(:age, 31) +cursor.bind_param(:notes, 'I like horses') +cursor.exec +con.commit +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/insert/template.rb b/ruby/CRUD-examples/insert/template.rb new file mode 100644 index 00000000..88f74b74 --- /dev/null +++ b/ruby/CRUD-examples/insert/template.rb @@ -0,0 +1,30 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. +# Code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ +# section titled "Boilerplate template" + +require 'oci8' + +def get_all_rows(label) + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Your code here + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/reset_data.rb b/ruby/CRUD-examples/reset_data.rb new file mode 100644 index 00000000..a37515bf --- /dev/null +++ b/ruby/CRUD-examples/reset_data.rb @@ -0,0 +1,45 @@ +# The following resets the data to the initial state for the tutorial. +# code Sample from the tutorial at https://learncodeshare.net/2016/08/26/basic-crud-operations-using-ruby-oci8/ + +# Query all rows +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse("delete from lcs_pets") +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse("alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 3)") +cursor.exec + +# Delete rows +cursor = con.parse("delete from lcs_people") +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse("alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)") +cursor.exec + +# Insert default people rows +cursor = con.parse("INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)") +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, ["Bob", "Kim"]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ["I like dogs", "I like birds"]) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse("INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)") +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, ["Duke", "Pepe"]) +cursor.bind_param_array(:owner, [1, 2]) +cursor.bind_param_array(:type, ["dog", "bird"]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/select/bind_variables.rb b/ruby/CRUD-examples/select/bind_variables.rb new file mode 100644 index 00000000..21e216e0 --- /dev/null +++ b/ruby/CRUD-examples/select/bind_variables.rb @@ -0,0 +1,19 @@ +# The example code below executes a simple query using named bind variables, +# uses fetch to retrieve the data and displays the results. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Select specific rows" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Example code showing a simple query using named bind variables. +person_name = 'Kim' +statement = 'select id, name, age, notes from lcs_people where name=:name' +cursor = con.parse(statement) +cursor.bind_param('name', person_name) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end +# End Example diff --git a/ruby/CRUD-examples/select/extra_fun_1.rb b/ruby/CRUD-examples/select/extra_fun_1.rb new file mode 100644 index 00000000..a2fbd24d --- /dev/null +++ b/ruby/CRUD-examples/select/extra_fun_1.rb @@ -0,0 +1,17 @@ +# The example code below executes a simple query, uses fetch to retrieve the data +# and displays the results. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Example code showing a simple query. +statement = 'select id, name, age, notes from lcs_people order by age' +cursor = con.parse(statement) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end +# End Example diff --git a/ruby/CRUD-examples/select/extra_fun_2.rb b/ruby/CRUD-examples/select/extra_fun_2.rb new file mode 100644 index 00000000..b84c5eba --- /dev/null +++ b/ruby/CRUD-examples/select/extra_fun_2.rb @@ -0,0 +1,18 @@ +# The example code below executes a simple query using named bind variables, +# uses fetch to retrieve the data and displays the results. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Example code showing a simple query using named bind variables. +statement = 'select id, name, age, notes from lcs_people where age > :age' +cursor = con.parse(statement) +cursor.bind_param('age', 30) +cursor.exec +cursor.fetch do |row| + printf "Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] +end +# End Example diff --git a/ruby/CRUD-examples/select/simple.rb b/ruby/CRUD-examples/select/simple.rb new file mode 100644 index 00000000..6440c28a --- /dev/null +++ b/ruby/CRUD-examples/select/simple.rb @@ -0,0 +1,18 @@ +# The example code below executes a simple query, uses fetch to retrieve the data +# and displays the results. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ +# section titled "Simple query" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Example code showing a simple query for all rows. +con = OCI8.new(connectString) +statement = 'select id, name, age, notes from lcs_people' +cursor = con.parse(statement) +cursor.exec +cursor.fetch do |row| + print row +end +# End Example diff --git a/ruby/CRUD-examples/select/template.rb b/ruby/CRUD-examples/select/template.rb new file mode 100644 index 00000000..53f06a02 --- /dev/null +++ b/ruby/CRUD-examples/select/template.rb @@ -0,0 +1,9 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. +# Code Sample from the tutorial at https://learncodeshare.net/2016/09/09/select-crud-using-ruby-oci8/ + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Your code here diff --git a/ruby/CRUD-examples/update/extra_fun_1.rb b/ruby/CRUD-examples/update/extra_fun_1.rb new file mode 100644 index 00000000..1717747f --- /dev/null +++ b/ruby/CRUD-examples/update/extra_fun_1.rb @@ -0,0 +1,45 @@ +# The example code below executes a simple update using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Extra Fun 1" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing a simple update using named bind variables. +statement = 'update lcs_people set notes = :notes where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:notes, 'I like cats') +cursor.bind_param(:id, 1) +cursor.exec +con.commit +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/extra_fun_2.rb b/ruby/CRUD-examples/update/extra_fun_2.rb new file mode 100644 index 00000000..511fd57e --- /dev/null +++ b/ruby/CRUD-examples/update/extra_fun_2.rb @@ -0,0 +1,50 @@ +# The example code below executes a simple update using named bind variables +# and displays the number of affected rows. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Extra Fun 2" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Example code showing a simple update using named bind variables +# and displays the number of affected rows. +statement = 'update lcs_pets set owner = :newOwner where type = :type and owner != :oldOwner' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:type, 'bird') +cursor.bind_param(:oldOwner, 2) +changed = cursor.exec +con.commit +# End Example + +printf "Number of rows updated: %d\n\n", changed + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/update/reset_data.rb b/ruby/CRUD-examples/update/reset_data.rb new file mode 100644 index 00000000..c9094cb9 --- /dev/null +++ b/ruby/CRUD-examples/update/reset_data.rb @@ -0,0 +1,49 @@ +# The following resets the data for use with the update section +# For both tables: +# Table data is removed. +# The identity column is set to start with the id after the starting data. +# Using the executemany function an array of starting data is inserted into the table. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Resetting the data" + +require 'oci8' +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +# Delete rows +cursor = con.parse('delete from lcs_pets') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_pets modify id generated BY DEFAULT as identity (START WITH 8)') +cursor.exec + +# Delete rows +cursor = con.parse('delete from lcs_people') +cursor.exec + +# Reset Identity Coulmn +cursor = con.parse('alter table lcs_people modify id generated BY DEFAULT as identity (START WITH 3)') +cursor.exec + +# Insert default people rows +cursor = con.parse('INSERT INTO lcs_people(id, name, age, notes) VALUES (:id, :name, :age, :notes)') +cursor.max_array_size = 2 +cursor.bind_param_array(:id, [1, 2]) +cursor.bind_param_array(:name, %w[Bob Kim]) +cursor.bind_param_array(:age, [35, 27]) +cursor.bind_param_array(:notes, ['I like dogs', 'I like birds']) +people_row_count = cursor.exec_array +printf " %d people rows inserted\n", people_row_count + +# Insert default pet rows +cursor = con.parse('INSERT INTO lcs_pets(id, name, owner, type) VALUES (:id, :name, :owner, :type)') +cursor.max_array_size = 7 +cursor.bind_param_array(:id, [1, 2, 3, 4, 5, 6, 7]) +cursor.bind_param_array(:name, %w[Duke Pepe Princess Polly Rollo Buster Fido]) +cursor.bind_param_array(:owner, [1, 2, 1, 1, 1, 1, 1]) +cursor.bind_param_array(:type, %w[dog bird snake bird horse dog cat]) +pet_row_count = cursor.exec_array +printf " %d pet rows inserted\n", pet_row_count + +con.commit diff --git a/ruby/CRUD-examples/update/simple.rb b/ruby/CRUD-examples/update/simple.rb new file mode 100644 index 00000000..002be13c --- /dev/null +++ b/ruby/CRUD-examples/update/simple.rb @@ -0,0 +1,45 @@ +# The example code below executes a simple update using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Simple update" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Example code showing a simple update using named bind variables. +statement = 'update lcs_people set age = :age where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:age, 31) +cursor.bind_param(:id, 1) +cursor.exec +con.commit +# End Example + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/specific_where.rb b/ruby/CRUD-examples/update/specific_where.rb new file mode 100644 index 00000000..48150a60 --- /dev/null +++ b/ruby/CRUD-examples/update/specific_where.rb @@ -0,0 +1,46 @@ +# The example code below executes a simple update using named bind variables. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Make sure your where clause is specific" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Example code showing a simple update using named bind variables. +statement = 'update lcs_pets set owner = :newOwner where owner = :oldOwner and type = :type' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:oldOwner, 1) +cursor.bind_param(:type, 'dog') +cursor.exec +con.commit +# End Example + +get_all_rows('New Data', 'pets') diff --git a/ruby/CRUD-examples/update/template.rb b/ruby/CRUD-examples/update/template.rb new file mode 100644 index 00000000..3296b89b --- /dev/null +++ b/ruby/CRUD-examples/update/template.rb @@ -0,0 +1,39 @@ +# The following code is used as the base template for the other examples. +# It is intended to be helper code not part of the examples. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Boilerplate template" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data') + +# Your code here + +get_all_rows('New Data') diff --git a/ruby/CRUD-examples/update/verify_number.rb b/ruby/CRUD-examples/update/verify_number.rb new file mode 100644 index 00000000..d3ee4d29 --- /dev/null +++ b/ruby/CRUD-examples/update/verify_number.rb @@ -0,0 +1,49 @@ +# The example code below executes a simple update using named bind variables +# and displays the number of affected rows. +# Code Sample from the tutorial at https://learncodeshare.net/2016/11/04/update-crud-using-ruby-oci8/ +# section titled "Verify the number of affected rows" + +require 'oci8' + +def get_all_rows(label, data_type = 'people') + connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database + con = OCI8.new(connectString) + + # Query all rows + statement = 'select id, name, age, notes from lcs_people order by id' + + if data_type == 'pets' + statement = 'select id, name, owner, type from lcs_pets order by owner, id' + end + + cursor = con.parse(statement) + cursor.exec + printf " %s:\n", label + cursor.fetch do |row| + if data_type == 'people' + printf " Id: %d, Name: %s, Age: %d, Notes: %s\n", row[0], row[1], row[2], row[3] + else + printf " Id: %d, Name: %s, Owner: %d, Type: %s\n", row[0], row[1], row[2], row[3] + end + end + printf "\n" +end + +connectString = ENV['DB_CONNECT'] # The environment variable for the connect string: DB_CONNECT=user/password@database +con = OCI8.new(connectString) + +get_all_rows('Original Data', 'pets') + +# Example code showing a simple update using named bind variables +# and displays the number of affected rows. +statement = 'update lcs_pets set owner = :newOwner where id = :id' +cursor = con.parse(statement) +cursor.bind_param(:newOwner, 2) +cursor.bind_param(:id, 6) +changed = cursor.exec +con.commit +# End Example + +printf "Number of rows updated: %d\n\n", changed + +get_all_rows('New Data', 'pets')