-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSV2SQLInsert.rb
More file actions
34 lines (24 loc) · 891 Bytes
/
CSV2SQLInsert.rb
File metadata and controls
34 lines (24 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env ruby
require 'csv'
csv_file = ARGV[0]
basename = File.basename csv_file, '.csv'
seperator = ARGV[1] || ','
raise 'You mass pass the CSV file as an arg' if csv_file.nil?
csv_text = File.read(csv_file)
# csv_text = csv_text.gsub(/(?<!\\)""/,'\\"') # converts "" to \"
csv = CSV.parse(csv_text, :col_sep => seperator, :headers => true, :write_headers => false, :force_quotes => true)
raise "You'll need header for this to work!" if csv.headers.nil?
output = "INSERT INTO #{basename} \n"
output << " (" + csv.headers.join(', ') + ") \n"
output << "VALUES \n"
# puts csv.methods
csv.each_with_index do |row,index|
output << ' ("'
tmp_arr = []
row.each do |field|
tmp_arr << field.last.gsub(/"/,'""')
end
output << tmp_arr.join('","')
output << '")' + (index < csv.size-1 ? ',' : ';') + " \n"
end
File.open(basename + '.sql', 'w') {|f| f.write(output) }