Skip to content

Instantly share code, notes, and snippets.

@hattwj
Last active December 22, 2015 11:39
Show Gist options
  • Save hattwj/6467273 to your computer and use it in GitHub Desktop.
Save hattwj/6467273 to your computer and use it in GitHub Desktop.
Ruby: Convert a CSV file into SQL (PostgreSQL tested)
#!/usr/bin/env ruby
require 'csv'
require 'cgi'
require 'rubygems'
require 'ruby-debug'
def usage
puts 'Usage:'
puts ' csv_to_sql.rb [no_transaction] tablename csvfile [tablename csvfile ...]'
exit 1
end
# Convert csv file to sql syntax and output to STDOUT
def convert_csv(path,table_name, in_transaction)
# csv file has a header row
header_row = true
# delimiter for csv file
delimiter = ','
# variable used to skip header row
skip_row = true
# Place sql statement in a transaction
puts 'BEGIN TRANSACTION;' if in_transaction == true
CSV.open(path, 'r', delimiter ) do |row|
row.each_with_index do |col,index|
if col==nil then
row[index]='NULL'
else
col.gsub!("'","`")
row[index] = "'#{ col }'"
end
end
if header_row
row.each_with_index do |col,index|
row[index] = "\"#{ col.gsub("'",'').gsub(/\s+/,'') }\""
end
header_row = false
print "INSERT INTO #{table_name} (#{row.join delimiter }) VALUES"
next
end
# Only skip the first (header) row
if skip_row == true
# dont skip further rows
skip_row = false
else
# Print end of line insert delimeter
puts delimiter
end
# Print to stdout
print "(#{row.join delimiter})"
end
puts ';'
puts 'COMMIT;' if in_transaction == true
end
transaction = true
# Check for transaction option and remove it from the arg stack if present
if ARGV.size > 0 && ARGV.include?('no_transaction')
ARGV.shift
transaction = false
end
# require correct parameters
if ARGV.size < 2
usage
end
# multiple sets of files/tables can be included in the same transaction, but they each need a table=>file
if ARGV.size % 2 !=0
puts 'Error: Must have an even number of parameters'
usage
end
# Run commands
(0..ARGV.size-1).step(2){|i|
convert_csv ARGV[i+1], ARGV[i], transaction
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment