Skip to content

Instantly share code, notes, and snippets.

@katmutua
Last active December 8, 2015 14:09
Show Gist options
  • Select an option

  • Save katmutua/620e95a96a3dee36537c to your computer and use it in GitHub Desktop.

Select an option

Save katmutua/620e95a96a3dee36537c to your computer and use it in GitHub Desktop.
Connect to DB2 and output the results to a csv file.
require 'rubygems'
# require 'mswin32/ibm_db'
# for windows
require 'ibm_db'
# for linux
require 'date.rb'
t=Time.now
lines=[]
filename = 'c://Temp//' +t.strftime("%m%d%Y_%H%M%S")+'.txt'
conn = IBM_DB.connect("DRIVER={IBM DB2 JDBC DRIVER};DATABASE=DBNAME;\
HOSTNAME=999.999.999.999;PORT=50004;PROTOCOL=TCPIP;\
UID=userid;PWD=password;", "", "")
sql = "SELECT address_id, addr_line_one FROM address fetch first 100 rows only"
stmt = IBM_DB.exec(conn, sql)
while row = IBM_DB.fetch_array(stmt)
lines << "#{row[0]},#{row[1]}"
end
# Output resultset to a CSV file
File.new(filename ,"w")
lines.each do |row|
File.open(filename ,"w") do |the_file|
0.upto(lines.size) { |i| the_file.puts "#{lines[i]}" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment