Guest User

Untitled

a guest
Jul 23rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. # Need the DateTime library.
  4. require "date"
  5. require "mysql"
  6.  
  7. # Declaring some needed arrays for manipulating data.
  8. input_file_line = Array.new
  9. output_file_line = Array.new
  10. input_file_headline = Array.new
  11.  
  12. # Name of the SQL database and table
  13. sql_hostname = "****"
  14. sql_username = "****"
  15. sql_password = "****"
  16. sql_database = "****"
  17. sql_table = "****"
  18.  
  19. # Connecting to database
  20. my = Mysql.connect(sql_hostname, sql_username, sql_password, sql_database)
  21.  
  22. # Opening the file streaming. Need to specify the path/filename.csv here.
  23. input_filename = "test.csv"
  24. input_file_stream = File.open(input_filename, "r")
  25.  
  26. # This line stats parsing the file. First line should be the login line.
  27. input_file_headline = input_file_stream.gets.to_s.split(/,/)
  28. login_id = input_file_headline[14].match(/[0-9]+/).to_s
  29.  
  30. # Output file creation
  31. #output_filename = Date.parse(input_file_headline[15]).to_s + "_" + login_id + ".sql"
  32. output_filename = Time.now.strftime("%Y-%m-%d").to_s + ".sql"
  33. output_file = File.open(output_filename, 'w')
  34.  
  35. # Writing input file headling to output file. Taking the first action from the file.
  36. output_file_line = "INSERT INTO " + sql_table + " (created_at, updated_at, date,phone_id, action, position_id) VALUES (\"" + Time.now.strftime("%Y-%m-%d %H:%M:%S") + "\",\"" + updated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S") + "\",\"" + Date.parse(input_file_headline[15]).to_s + " " + input_file_headline[16].gsub(/"/, "") + "\"," + login_id + "," + input_file_headline[18] + ", " + input_file_headline[19] + ");\r\n"
  37. output_file.write(output_file_line)
  38. #my.query(output_file_line)
  39.  
  40. # Looping through the input file. Taking values needed and writing to output file. Ends on EOF.
  41. begin
  42. input_file_line = input_file_stream.gets.to_s.split(/,/)
  43. output_file_line = "INSERT INTO " + sql_table + " (created_at, updated_at, date, phone_id, action, position_id) VALUES (\"" + Time.now.strftime("%Y-%m-%d %H:%M:%S") + "\",\"" + updated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S") + "\",\"" + Date.parse(input_file_line[14]).to_s + " " + input_file_line[15].gsub(/"/, "") + "\"," + login_id + "," + input_file_line[17] + ", " + input_file_line[18] + ");\r\n"
  44. output_file.write(output_file_line)
  45. #my.query(output_file_line)
  46. #puts input_file_line[24]
  47.  
  48. end while !input_file_stream.eof?
  49. #end while (input_file_line[24].empty? == false)
  50. puts input_file_line[24].empty?
  51.  
  52. # Closing the streamings.
  53. input_file_stream.close()
  54. output_file.close()
  55.  
  56. puts ">> File " + input_filename + " parsed and imported successfully."
  57. puts ">> File " + output_filename + " created for future use."
Add Comment
Please, Sign In to add comment