MySQL - Ruby Interface - Examples


List Databases

require "mysql"
m = Mysql.new(host, user, passwd)
m.list_dbs.each do |db|
  puts db
end

List Tables

require "mysql"
m = Mysql.new(host, user, passwd, db)
m.list_tables.each do |table|
  puts table
end

List Field name

require "mysql"
m = Mysql.new(host, user, passwd, db)
m.list_fields("table").fetch_fields.each do |f|
  puts f.name
end

Select Data

require "mysql"
m = Mysql.new(host, user, passwd, db)
res = m.query("select * from table")
fields = res.fetch_fields.filter do |f| f.name end
puts fields.join("\t")
res.each do |row|
  puts row.join("\t")
end

tommy@tmtm.org
Last modified: Sat Sep 15 02:31:14 JST 2001