Ruby/DBI数据库访问接口
Ruby/DBI 是一个跟 Perl/DBI 类似的统一数据库访问接口,支持各种流行的数据库。
下面是一段使用了 Ruby/DBI 的代码:
#!/usr/bin/ruby -w
# simple.rb - simple MySQL script using Ruby DBI module
require "dbi"
begin
# connect to the MySQL server
dbh = DBI.connect("DBI:Mysql:test:localhost", "testuser", "testpass")
# get server version string and display it
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# disconnect from server
dbh.disconnect if dbh
end
评论