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.
This program is under Ruby's license.
% ruby ./setup.rb % ruby ./test.rb hostname user passwd # ruby ./install.rb
% 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.
% 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.
# 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.
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
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
return Mysql class object.
connect to mysqld and return Mysql class object.
quote string for insert, update. Don't use for multibyte charset.
return client version information.
unsupported.
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.
same as Mysql::real_connect().
return number of affected rows.
change user.
unsupported.
close connection.
create database.
drop database.
same as C API mysql_dump_debug_info().
return error number.
return error message.
quote string for insert, update. Don't use for multibyte charset.
return number of field for last query.
return connection information.
return protocol version.
return server version information.
return query information. if no information, return nil.
last generated AUTO_INCREMENT-ed field's value.
kill thread for id.
return array of database name.
return Mysql::Result class object that indicate field information.
return Mysql::Result class object that indicate thread list on server.
return array of table name.
check that server is alive.
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.
If true, query() also invokes store_result() and returns a MysqlRes object. Default is true.
do query only. store_result() is not invoked.
flush server log or cache.
reload grant table.
select database.
shutdown server.
return server status.
return Mysql::Result class object.
return thread id of this connection.
return Mysql::Result class object.
query result class. MysqlRes is alias name for compatibility.
set current row position to offset line.
repeat fetch_row().
repeat fetch_hash().
return Mysql::Field class object.
return array of Mysql::Field class object.
return nth Mysql::Field class object.
return array of length of field value of current row.
return array of field value.
return hash of pair of field name and field value. if with_table is true, hash key is table+"."+fieldname.
set current row position to offset line.
return current row position.
call GC::start.
return number of fields.
return number of rows.
same as data_seek().
return current row position.
field information class. MysqlField is alias for compatibility.
field name.
table name.
default value.
type of field.
field length.
maximum length of field of result set.
flag.
number of decimal.
return hash. obj.hash['name'] == obj.name, obj.hash['type'] == obj.type, ...
MySQL error class. MysqlError is alias for compatibility.
error number.
error message.
TOMITA Masahiro tommy@tmtm.org http://www.tmtm.org