Skip to content

Instantly share code, notes, and snippets.

@masaki
Created December 11, 2011 12:25
Show Gist options
  • Save masaki/1460328 to your computer and use it in GitHub Desktop.
Save masaki/1460328 to your computer and use it in GitHub Desktop.
MySQL Casual 2011 sample
require 'rubygems'
require 'dbi'
require 'pp'
dbh = DBI.connect('dbi:mysql:database=test;host=localhost', 'test', 'test')
pp dbh.select_one('SELECT VERSION()')
sth = dbh.prepare('SELECT * FROM mysql_casual WHERE name = ?')
sth.execute('ikasam_a')
while row = sth.fetch_hash do
pp row
end
sth.finish
dbh.disconnect
require 'rubygems'
require 'dbi'
require 'test/mysqld'
require 'pp'
module Test
Mysqld.class_eval do
def username
dsn[:username]
end
def dbi_dsn
"dbi:Mysql:database=#{dsn[:database]};socket=#{dsn[:socket]}"
end
end
end
mysqld = Test::Mysqld.new(:auto_start => true)
pp mysqld.dbi_dsn
# "dbi:Mysql:database=test;socket=/tmp/11wkcy6n/tmp/mysql.sock"
dbh = DBI.connect(mysqld.dbi_dsn, mysqld.username)
# ...
require 'rubygems'
require 'mysql2'
require 'test/mysqld'
require 'pp'
mysqld = Test::Mysqld.new(:auto_start => true)
pp mysqld.dsn
# {:port=>13306,
# :socket=>"/tmp/11wkcy6n/tmp/mysql.sock",
# :username=>"root",
# :database=>"test"}
client = Mysql2::Client.new(mysqld.dsn)
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment