Ruby/MySQL

[Japanese page]


This is the MySQL Ruby API written by Ruby. It provides the same functions for Ruby programs that the MySQL C API provides for C programs.

Currently, this cannot be used for MySQL 4.1.x.

Requirement

License

This program is under Ruby's license.

Download

Install

% ruby ./setup.rb
% ruby ./test.rb hostname user passwd
# ruby ./install.rb

Setup

% ruby ./setup.rb

This command find MySQL's UNIX domain socket. This is not needed for system that does not have UNIX domain socket such as Microsoft Windows.

Test

% ruby ./test.rb hostname user passwd

test.rb should be invoked with three arguments that indicate the MySQL server hostname, and the username and password for a MySQL account that can create a database named "rubytest". An optional fourth argument is allowed, to specify a database name to use rather than "rubytest". The database should not already exist.

Install

# ruby ./install.rb

This command install mysql.rb to standard library directory(Config::CONFIG["rubylibdir"]). If you want to copy to other directory, copy mysql.rb to appropriate directory.

Missing Features

Usage

example)

require "mysql"
my = Mysql::new("host", "user", "passwd", "db")
res = my.query("select * from tbl")
res.each do |row|
  col1 = row[0]
  col2 = row[1]
end

Reference

The names of methods provided by this module basically are the same as the names of the functions in the C API, except that the Ruby method names do not begin with a 'mysql_' prefix. For example, the Ruby query() method corresponds to the C API mysql_query() function. For details on the use of each Ruby method, see the descriptions of the corresponding C functions in the MySQL Reference Manual.

Some Ruby methods may be invoked under other names that serve as equivalent aliases, as noted below.

If an error occurs when a method executes, it raises a Mysql::Error exception.

Mysql class

MySQL class

Class Methods

init()

return Mysql class object.

real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
new(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)

connect to mysqld and return Mysql class object.

escape_string(str)
quote(str)

quote string for insert, update. Don't use for multibyte charset.

get_client_info()
client_info()

return client version information.

debug(str)

unsupported.

Object Methods

options(opt, val=nil)

set options. opt is available only Mysql::OPT_LOCAL_INFILE. if val is omitted or val is true value, LOAD DATA LOCAL INFILE query can be used.

real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)
connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)

same as Mysql::real_connect().

affected_rows()

return number of affected rows.

change_user(user=nil, passwd=nil, db=nil)

change user.

character_set_name()

unsupported.

close()

close connection.

create_db(db)

create database.

drop_db(db)

drop database.

dump_debug_info()

same as C API mysql_dump_debug_info().

errno()

return error number.

error()

return error message.

escape_string(str)
quote(str)

quote string for insert, update. Don't use for multibyte charset.

field_count()

return number of field for last query.

get_host_info()
host_info()

return connection information.

get_proto_info()
proto_info()

return protocol version.

get_server_info()
server_info()

return server version information.

info()

return query information. if no information, return nil.

insert_id()

last generated AUTO_INCREMENT-ed field's value.

kill(id)

kill thread for id.

list_dbs(db=nil)

return array of database name.

list_fields(table, field=nil)

return Mysql::Result class object that indicate field information.

list_processes()

return Mysql::Result class object that indicate thread list on server.

list_tables(table=nil)

return array of table name.

ping()

check that server is alive.

query(q)

do query. If query return result, automatically invoke store_result() and return Mysql::Result object when query_with_result is true. If query do not return result, return nil.

query_with_result
query_with_result=bool

If true, query() also invokes store_result() and returns a MysqlRes object. Default is true.

real_query(q)

do query only. store_result() is not invoked.

refresh(r)

flush server log or cache.

reload()

reload grant table.

select_db(db)

select database.

shutdown()

shutdown server.

stat()

return server status.

store_result()

return Mysql::Result class object.

thread_id()

return thread id of this connection.

use_result()

return Mysql::Result class object.

Mysql::Result class

query result class. MysqlRes is alias name for compatibility.

Object Methods

data_seek(offset)

set current row position to offset line.

each() {|x| ...}

repeat fetch_row().

each_hash(with_table=false) {|x| ...}

repeat fetch_hash().

fetch_field()

return Mysql::Field class object.

fetch_fields()

return array of Mysql::Field class object.

fetch_field_direct(n)

return nth Mysql::Field class object.

fetch_lengths()

return array of length of field value of current row.

fetch_row()

return array of field value.

fetch_hash(with_table=false)

return hash of pair of field name and field value. if with_table is true, hash key is table+"."+fieldname.

field_seek(offset)

set current row position to offset line.

field_tell()

return current row position.

free()

call GC::start.

num_fields()

return number of fields.

num_rows()

return number of rows.

row_seek(offset)

same as data_seek().

row_tell()

return current row position.

Mysql::Field class

field information class. MysqlField is alias for compatibility.

Object Methods

name()

field name.

table()

table name.

def()

default value.

type()

type of field.

length()

field length.

max_length()

maximum length of field of result set.

flags()

flag.

decimals()

number of decimal.

hash()

return hash. obj.hash['name'] == obj.name, obj.hash['type'] == obj.type, ...

Mysql::Error class

MySQL error class. MysqlError is alias for compatibility.

Object Methods

errno()

error number.

error()

error message.

History

2005-02-12 ver. 0.2.6
2004-10-07 ver. 0.2.5
2004-01-21 ver. 0.2.4
2004-01-11 ver. 0.2.3a
2004-01-11 ver. 0.2.3
2003-10-21 ver. 0.2.2
2003-08-27 ver. 0.2.1
2003-08-19 ver. 0.2.0
2003-08-16 ver. 0.1.5
2003-08-11 ver. 0.1.4
2003-07-27 ver. 0.1.3
2003-07-25 ver. 0.1.2
2003-07-24 ver. 0.1.1
2003-07-23 ver. 0.1

Author

TOMITA Masahiro tommy@tmtm.org http://www.tmtm.org


TOMITA Masahiro
Last modified: Sat Jun 10 16:12:15 JST 2006