ruby-mysql
Description
ruby-mysql is a MySQL client library.
It is written entirely in Ruby.
Therefore libmysqlclient is not required and no compilation is required during installation.
Installation
gem install ruby-mysql
Synopsis
require 'mysql'
my = Mysql.connect('mysql://username:password@hostname:port/dbname?charset=utf8mb4')
my.query("select col1, col2 from tblname").each do |col1, col2|
p col1, col2
end
stmt = my.prepare('insert into tblname (col1,col2) values (?,?)')
stmt.execute 123, 'abc'
Major incompatibility with 3.0
Result values are now converted by default
| NULL | NilClass |
| INT | Integer |
| DECIMAL | BigDecimal |
| FLOAT, DOUBLE | Float |
| DATE | Date |
| DATETIME, TIMESTAMP | Time |
| TIME | Float (as seconds) |
| YEAR | Integer |
| CHAR, VARCHAR | String |
| BIT | String |
| TEXT, BLOB, JSON | String |
3.0:
pp my.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
4.0:
pp my.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
To specify cast: false, you get the same behavior as in 3.0.
my.query('select 123,123.45,now(),cast(now() as date)', cast: false).fetch.map{[_1, _1.class]}
It can also be specified during Mysql.new and Mysql.connect.
my = Mysql.connect('mysql://user:pass@localhost/', cast: false)
Changing mysql.default_options will affect the behavior of subsequently created instances.
my1 = Mysql.connect('mysql://user:pass@localhost/')
Mysql.default_options[:cast] = false
my2 = Mysql.connect('mysql://user:pass@localhost/')
pp my1.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
pp my2.query('select 123,123.45,now(),cast(now() as date)').fetch.map{[_1, _1.class]}
Mysql::Result#each now always return records from the beginning
3.0:
res = my.query('select 123 union select 456')
res.entries
res.entries
4.0:
res = my.query('select 123 union select 456')
res.entries
res.entries
Copyright
- Author: TOMITA Masahiro tommy@tmtm.org
- Copyright: Copyright 2008 TOMITA Masahiro
- License: MIT