Last active
December 29, 2015 22:20
-
-
Save toronya/4592767 to your computer and use it in GitHub Desktop.
simple example of DB table to csv (oracle)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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