Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- require "mysql"
- INSERT_COUNT = 1000000;
- HOSTNAME = 'localhost'
- USERNAME = 'username'
- PASSWORD = 'password'
- DATABASE = 'database'
- PORT = 3306
- begin
- # Connect MySQL
- db = Mysql::connect(HOSTNAME, USERNAME, PASSWORD, DATABASE, PORT)
- # DB prepare
- sth = db.prepare("insert into drive_log (col_1, col_2, col_3, created_at, updated_at) values (?, ?, ?, ?, ?);")
- db.autocommit(false)
- INSERT_COUNT.times do |i|
- col_1 = 1
- col_2 = 2
- col_3 = 3
- created_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")
- updated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S")
- sth.execute(col_1,
- col_3,
- col_3,
- created_at,
- updated_at)
- db.commit if (i+1) % 10000 == 0
- p "#{i+1}回目です。" if (i+1) % 100 == 0
- end
- rescue Mysql::Error => e
- db.rollback
- p "Error message: #{e.error}, code: #{e.errno}"
- ensure
- db.close if db
- end
Add Comment
Please, Sign In to add comment