forked from ethbux1/capitalgains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.rb
34 lines (29 loc) · 924 Bytes
/
import.rb
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
34
require 'csv'
require 'mysql2'
require 'time'
# Usage: "ruby ./import.rb"
# This imports the master.csv into the database, skipping duplicates.
@client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "capitalgains")
@data = CSV.parse(File.read('trades.csv'))
n = 0;
@data.drop(1).each do |row|
next if row[0].nil?
begin
statement = @client.prepare("INSERT INTO trades (remote_id, marketplace, product, buy, volume, price, fee, total, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);");
result = statement.execute(
row[0],
row[1],
row[2],
row[3],
row[4],
row[5],
row[6],
row[7],
Time.parse(row[8])
)
n += 1
rescue Exception => e
puts e.to_s + " row=" + row.join(',')
end
end
puts "Imported " + n.to_s + " rows successfully."