Last active
December 8, 2015 14:09
-
-
Save katmutua/620e95a96a3dee36537c to your computer and use it in GitHub Desktop.
Connect to DB2 and output the results to a csv file.
This file contains hidden or 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
| 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