解决方法
从这里:
http://github.com/rails-sqlserver/2000-2005-adapter/tree/master
安装
首先,您将需要Ruby DBI和Ruby ODBC.据我所知,不再支持ADO DBD for DBI.下面的安装不是一个全面的步骤,通过如何获取所有必需的移动部件,如FreeTDS安装和/或配置.它还将假设从属库和适配器本身的gem安装.
应该注意的是,该版本的适配器是使用古老的0.0.23版本的DBI开发的,直到目前的稳定版本为0.4.0.因为后期版本的DBI将会改变很多事情,所以很高兴地建议您将安装的最大值设置为0.4.0,这些示例如下所示.目前我们不支持高于0.4.0的DBI版本.好消息是,如果您使用非常旧的DBI与ADO,技术上这个适配器仍然可以为您工作,但要警告您的路径越来越老,可能不支持长时间.
$gem install dbi --version 0.4.0 $gem install dbd-odbc --version 0.2.4 $gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com
从这里:http://lambie.org/2008/02/28/connecting-to-an-mssql-database-from-ruby-on-ubuntu/
export ODBCINI=/etc/odbc.ini export ODBCSYSINI=/etc export FREETDSCONF=/etc/freetds/freetds.conf
然后重新加载你的.profile,注销并重新输入.
其次,在Ubuntu 7.10服务器上,我需要安装一些软件包.
mlambie@ubuntu:~$sudo aptitude install unixodbc unixodbc-dev freetds-dev sqsh tdsodbc
安装FreeTDS我可以这样配置:
mlambie@ubuntu:/etc/freetds$cat freetds.conf [ACUMENSERVER] host = 192.168.0.10 port = 1433 tds version = 7.0
这里重要的是ACUMENSERVER,它是连接到数据库时使用的DSN.主机和端口是不言自明的,值得注意的是,我不得不使用7.0作为tds版本.
测试FreeTDS不是太难:
mlambie@ubuntu:~$sqsh -S ACUMENSERVER -U username -P password sqsh: Symbol `_XmStrings' has different size in shared object,consider re-linking sqsh-2.1 Copyright (C) 1995-2001 Scott C. Gray This is free software with ABSOLUTELY NO WARRANTY For more information type '\warranty' 1> use acumen 2> go 1> select top 1 firstname,lastname from tblClients 2> go [record returned] (1 row affected) 1> quit
接下来需要配置ODBC:
mlambie@ubuntu:/etc$cat odbcinst.ini [FreeTDS] Description = TDS driver (Sybase/MS sql) Driver = /usr/lib/odbc/libtdsodbc.so Setup = /usr/lib/odbc/libtdsS.so CPTimeout = CPReuse = FileUsage = 1 mlambie@ubuntu:/etc$cat odbc.ini [ACUMENSERVER] Driver = FreeTDS Description = ODBC connection via FreeTDS Trace = No Servername = ACUMENSERVER Database = ACUMEN
然后我测试了与isql的连接:
mlambie@ubuntu:~$isql -v ACUMENSERVER username password +---------------------------------------+ | Connected! | | | | sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ sql> use ACUMEN [][unixODBC][FreeTDS][sql Server]Changed database context to 'Acumen'. [Isql]INFO: sqlExecute returned sql_SUCCESS_WITH_INFO sqlRowCount returns -1 sql> select top 1 firstname from tblClients; [record returned] sqlRowCount returns 1 1 rows fetched sql> quit
好的,所以我们有ODBC使用FreeTDS连接到一个远程的MSsql服务器.剩下的就是将Ruby添加到组合中.
mlambie@ubuntu:~$sudo aptitude install libdbd-odbc-ruby
最后要测试的是Ruby可以使用DBI和ODBC命中实际的数据库,这很容易测试:
mlambie@ubuntu:~$irb irb(main):001:0> require "dbi" => true irb(main):002:0> dbh = DBI.connect('dbi:ODBC:ACUMENSERVER','username','password') => #<DBI::DatabaseHandle:0xb7ac57f8 @handle=#<DBI::DBD::ODBC::Database:0xb7ac5744 @handle=#<odbc::database:0xb7ac576c>,@attr={}>,@trace_output=#</odbc::database:0xb7ac576c><io:0xb7cbff54>,@trace_mode=2> irb(main):003:0> quit
还有一个更完整的测试(只有使用sql SELECT,记住你):
#!/usr/bin/env ruby require 'dbi' db = DBI.connect('dbi:ODBC:ACUMENSERVER','password') select = db.prepare('SELECT TOP 10 firstname FROM tblClients') select.execute while rec = select.fetch do puts rec.to_s end db.disconnect </io:0xb7cbff54>
从这里(修复odbc lib在错误的地方):
http://ubuntuforums.org/showthread.php?t=433435&page=2
libtdsodbc.so with freeTDS (freetds-dev,tdsodbc),you can either edit the path in the odbcinst.ini file for the [FreeTDS] driver section OR cp the /usr/lib/odbc/libtdsodbc.so into /usr/lib/libtdsodbc.so.
isql -v $dsn $user $passwd
我发现这是有用的
http://www.unixodbc.org/doc/FreeTDS.html#Configuration
然后在database.yml文件中:
development: adapter: sqlserver mode: odbc dsn: dsn_name username: my_username password: my_password