因?yàn)楣ぷ餍枰?,要分析存放在SQL Server上的數(shù)據(jù),所以不得不研究一下如何使用Ruby訪問(wèn)SQL Server,發(fā)現(xiàn)其實(shí)還是很簡(jiǎn)單的:
安裝FreeTDS
下載FreeTDS源代碼
解壓編譯安裝:
復(fù)制代碼 代碼如下:
./configure --prefix=/usr/local/freetds make sudo make install
安裝Tiny_TDS
Tiny_TDS,安裝和使用非常簡(jiǎn)單,推薦使用:
復(fù)制代碼 代碼如下:
sudo gem install tiny_tds -- --with-freetds-dir=/usr/local/freetds
用tiny_tds訪問(wèn)SQL Server很簡(jiǎn)單:
復(fù)制代碼 代碼如下:
require 'tiny_tds'
client = TinyTds::Client.new(:username => 'fankai', :password => 'fankai', :host => '192.168.0.1', :database => 'test')
result = client.execute("select top 10 * from User");
result.each do |row|
puts row
end
在ActiveRecord上使用Tiny_TDS
這也非常簡(jiǎn)單,參考這個(gè)教程Using TinyTDS:
復(fù)制代碼 代碼如下:
gem install activerecord-sqlserver-adapter
配置database.yml如下:
復(fù)制代碼 代碼如下:
development:
adapter: sqlserver
host: mydb.net
database: myapp_development
username: sa
password: secret
您可能感興趣的文章:- 淺談Ruby on Rails下的rake與數(shù)據(jù)庫(kù)數(shù)據(jù)遷移操作
- Ruby連接使用windows下sql server數(shù)據(jù)庫(kù)代碼實(shí)例
- 在Ruby程序中連接數(shù)據(jù)庫(kù)的詳細(xì)教程
- ruby+nokogori抓取糗事百科前10頁(yè)并存儲(chǔ)進(jìn)數(shù)據(jù)庫(kù)示例
- Ruby on Rails框架程序連接MongoDB的教程