Guest User

Untitled

a guest
Oct 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. require "mysql"
  3.  
  4. INSERT_COUNT = 1000000;
  5. HOSTNAME = 'localhost'
  6. USERNAME = 'username'
  7. PASSWORD = 'password'
  8. DATABASE = 'database'
  9. PORT = 3306
  10.  
  11. begin
  12. # Connect MySQL
  13. db = Mysql::connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, PORT)
  14.  
  15. # DB prepare
  16. sth = db.prepare("insert into drive_log (col_1, col_2, col_3, created_at, updated_at) values (?, ?, ?, ?, ?);")
  17.  
  18. db.autocommit(false)
  19.  
  20. INSERT_COUNT.times do |i|
  21. col_1 = 1
  22. col_2 = 2
  23. col_3 = 3
  24. created_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  25. updated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  26. sth.execute(col_1,
  27. col_3,
  28. col_3,
  29. created_at,
  30. updated_at)
  31.  
  32. db.commit if (i+1) % 10000 == 0
  33. p "#{i+1}回目です。" if (i+1) % 100 == 0
  34. end
  35.  
  36. rescue Mysql::Error => e
  37. db.rollback
  38. p "Error message: #{e.error}, code: #{e.errno}"
  39. ensure
  40. db.close if db
  41. end
Add Comment
Please, Sign In to add comment