Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. require 'tiny_tds'
  2. require 'csv'
  3.  
  4. ## Connect to database
  5. client = TinyTds::Client.new username: '<db-user>', password: '<db-pass>', host: '<host or IP>', port: 1433, database: '<db-name>'
  6.  
  7. if client.active?
  8. puts "Connection established"
  9. end
  10.  
  11. ## Open and readCSV file
  12.  
  13. logs = CSV.read('test.csv')
  14. record = 1
  15. index = 0
  16.  
  17. ## Store basic information and loop through each record
  18.  
  19. logs[record].each do |value|
  20. host = logs[record][0]
  21. domain = logs[record][1]
  22. type = logs[record][2]
  23. server_type = logs[record][3]
  24.  
  25. ## Insert Host information to database and test by verifying the generated index number for the entry
  26. results = client.execute("INSERT dbo.Host_Name (Host_Name)
  27. OUTPUT INSERTED.Host_Name_Index VALUES ('#{host}' )")
  28. results.each(:as => :array) do |row|
  29. puts row
  30. index = row.pop
  31. end
  32.  
  33. results = client.execute("INSERT dbo.Domain (FQ_Domain_Name)
  34. OUTPUT INSERTED.Domain_Index VALUES ('#{domain}' )")
  35. results.each do |row|
  36. puts row
  37. end
  38.  
  39. if type == 'server'
  40. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  41. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},1)")
  42. results.each do |row|
  43. puts row
  44. end
  45. elsif type == 'desktop'
  46. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  47. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},2)")
  48. results.each do |row|
  49. puts row
  50. end
  51. else type == 'laptop'
  52. results = client.execute("INSERT dbo.Assets (Host_Name_Index, Type_Index)
  53. OUTPUT INSERTED.Device_Index VALUES (#{index.to_i},3)")
  54. results.each do |row|
  55. puts row
  56. end
  57. end
  58.  
  59. record += 1
  60. end
  61.  
  62. print 'Done processing csv file'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement