Ruby on Rails 的 dbi

ruby on rails 的dbi功能可以單獨建立database server 連線,而不靠rails 內建的 ActiveRecord進行撈資料的動作。但最近真的快被搞腦袋都冒煙,無論重裝幾次,ruby 都告訴你 dbi 都require不進去。

[cc lang=”bash”]irb(main):001:0 > require ‘dbi’
LoadError: no such file to load — dbi
from (irb):1:in `require’
from (irb):1
from :0
[/cc]

但是google之後,先加進 require ‘rubygems’ 就都true了。

[cc lang=”bash”]irb(main):002:0> require ‘rubygems’
=> true
irb(main):003:0> require ‘dbi’
=> true[/cc]

然後照google所給的code來試ruby 連線到 mysql

[cc lang=”ruby”] #!/usr/bin/ruby -w
# simple.rb – simple MySQL script using Ruby DBI module

require “rubygems”
require “dbi”
#require “mysql”

begin
# connect to the MySQL server
dbh = DBI.connect(“DBI:Mysql:test:localhost”, “root”, “xxxxx”)
# 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
[/cc]

倒是有看到連線成功的訊息

[cc]Server version: 5.1.54-1ubuntu4[/cc]

不過在那先前螫疼很久了,又在google上找到這行命令再安裝進去

[cc lang=”bash”]$ sudo apt-get install libmysql-ruby libmysqlclient-dev
$ sudo gem install mysql[/cc]

照文字模式的ruby是可以成功連線,但是!

用在ruby on rails上使用仍然出現

[cc lang=”bash”]:in `require’: no such file to load — dbi (LoadError) from[/cc]

無解…雪特!

Related Posts Plugin for WordPress, Blogger...

2 thoughts on “Ruby on Rails 的 dbi”

Leave a Reply