Skip to content

Instantly share code, notes, and snippets.

@yoonchulkoh
Created June 14, 2012 12:16
Show Gist options
  • Save yoonchulkoh/2929952 to your computer and use it in GitHub Desktop.
Save yoonchulkoh/2929952 to your computer and use it in GitHub Desktop.
テストデータinsert用スクリプト
# -*- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment