Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. require 'mysql2'
  2. require 'highline'
  3.  
  4. cli = HighLine.new
  5. password = cli.ask("MySQL password: ") { |q| q.echo = false }
  6. client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => password)
  7.  
  8. database = cli.ask("Database to create: ")
  9. table = cli.ask("Table to create: ")
  10. client.query("CREATE DATABASE IF NOT EXISTS #{database}")
  11. client.query(<<EOF)
  12. CREATE 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. )
  26. EOF
  27.  
  28. statement = client.prepare(<<EOF)
  29. INSERT INTO #{database}.#{table}
  30. (cluster_id, record_id, ssn, first, middle, last, street_num, street_name, apt, city, state, zip)
  31. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  32. EOF
  33. File.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
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement