Skip to content

Instantly share code, notes, and snippets.

@toronya
Last active December 29, 2015 22:20
Show Gist options
  • Save toronya/4592767 to your computer and use it in GitHub Desktop.
Save toronya/4592767 to your computer and use it in GitHub Desktop.
simple example of DB table to csv (oracle)
# coding:utf-8
# usage: ruby oracle_to_csv.rb > sample.csv
require 'rubygems'
require 'oci8' # before you need install : gem install ruby-oci8
def csvout(user, pass, world, sql)
begin
ora = OCI8.new(user,pass,world)
c = ora.exec(sql)
#HEADER
puts '"' + c.get_col_names.join('","').to_s + '"'
while r = c.fetch()
#BODY
puts '"' + r.join('","').to_s + '"'
end
ensure
c.close
ora.logoff
STDERR.puts ora.last_error unless ora.last_error.nil?
end
end
SQL = 'select * from DataTableName'
csvout('User', 'Password', 'DB', SQL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment