· 8 years ago · Oct 07, 2017, 09:44 AM
1require 'mysql2'
2require 'highline'
3
4cli = HighLine.new
5password = cli.ask("MySQL password: ") { |q| q.echo = false }
6client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => password)
7
8database = cli.ask("Database to create: ")
9table = cli.ask("Table to create: ")
10client.query("CREATE DATABASE IF NOT EXISTS #{database}")
11client.query(<<EOF)
12CREATE TABLE #{database}.#{table} (
13 cluster_id INT,
14 record_id INT,
15 ssn VARCHAR(9),
16 first VARCHAR(63),
17 middle VARCHAR(15),
18 last VARCHAR(63),
19 street_num VARCHAR(15),
20 street_name VARCHAR(63),
21 apt VARCHAR(15),
22 city VARCHAR(63),
23 state VARCHAR(2),
24 zip VARCHAR(5)
25)
26EOF
27
28statement = client.prepare(<<EOF)
29INSERT INTO #{database}.#{table}
30 (cluster_id, record_id, ssn, first, middle, last, street_num, street_name, apt, city, state, zip)
31VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
32EOF
33File.open("output.db", "r") do |f|
34 while !f.eof?
35 line = f.readline
36 args = line.split(":").collect { |col| col.empty? ? nil : col }
37 statement.execute(*args)
38 end
39end