Getting Rails connected to Microsoft SQL Server from OS X requires two things:
1) An ODBC driver manager – unixODBC and iODBC are the most popular.
2) An ODBC driver – Almost all solutions involve FreeTDS, an open source library that allows for connecting to MS SQL Server and Sybase.
You can install FreeTDS using MacPorts with:sudo port install freetds
You want to compile?
(Check here for help installing FreeTDS.)
Once FreeTDS is installed you can check your settings by running:
% tsql -C
My settings were:
Compile-time settings (established with the "configure" script):
Version: freetds v0.63
MS db-lib source compatibility: no
Sybase binary compatibility: unknown
Thread safety: yes
iconv library: yes
TDS version: 8.0
iODBC: yes
unixodbc: noNext you need to configure the following files as indicated: Note: If you did NOT install via MacPorts you probably use /usr/local.
/opt/local/etc/freetds/freetds.conf:
Add a section to the bottom of the file (remove the parenthetical comments):
[YOUR_DB_DEFINITION_NAME]
host = 192.168.1.101 (change this to the right one for you)
port = 1433
tds version = 4.2 (might be 8.0, check the docs for the version of SQL Server you are running)
TEST!
# tsql -S YOUR_DB_DEFINITION_NAME -U USERNAME -P PASSWORD
You should see and do the following to make sure all went well:
locale is "en_US.ISO-8859-15"
locale charset is "ISO-8859-15"
1> use YOUR_ACTUAL_DB_NAME
2> go
1> select * from users (do this for a table that exists in your db)
2> go
id login
1 user1
3 user2 (make sure you get correct output)
1> quit
You probably have to make the /Library/ODBC dir.
/Library/ODBC/odbcinst.ini:
[ODBC Drivers]
JDBC = Installed
[JDBC]
Description = Sybase JDBC Server
Driver = /usr/local/lib/libtdsodbc.so
Setup = /usr/local/lib/libtdsodbc.so
/Library/ODBC/odbc.ini:
[ODBC Data Sources]
MySQLServer = JDBC
[MyDSN]
Driver = /usr/local/lib/libtdsodbc.so
Description = Description of this database connection
Trace = yes
TraceFile = /tmp/odbc.log
Servername = MySQLServer
Database = YOUR_ACUTAL_DB_NAME
/Library/ODBC/odbcinst.ini:
[TDS]
Description = FreeTDS
Driver = /opt/local/lib/libtdsodbc.so
UsageCount = 1
/Library/ODBC/odbc.ini:
[YOUR_DB_DEFINITION_NAME]
Driver = TDS
Description = ODBC connection to via FreeTDS
Trace = No
Servername = YOUR_DB_DEFINITION_NAME
Database = YOUR_ACTUAL_DB_NAME
% iodbctest "dsn=MyDSN;uid=USERNAME;pwd=PASSWORD"
You should be able to run SQL commands at this point. If so, congratulations: you have a working DSN and are 80% of the way to getting Rails working with ODBC under OS X.
If you’re using MacPorts, you can do:sudo port install rb-odbc
sudo port install rb-dbi +dbd_odbc
Compilers, type away.cd ~/tmp
curl -O !http://www.ch-werner.de/rubyodbc/ruby-odbc-0.996.tar.gz
tar vxzf ruby-odbc-0.996.tar.gz
cd ruby-odbc-0.996
ruby extconf.rb
make
sudo make install
cd ~/tmp
curl -O <a href="http://rubyforge.org/frs/download.php/655/ruby-dbi-all-0.0.23.tar.gz">http://rubyforge.org/frs/download.php/655/ruby-dbi-all-0.0.23.tar.gz</a>
tar vxzf ruby-dbi-all-0.0.23.tar.gz
cd ruby-dbi-all
ruby setup.rb config --with=dbi,dbd_odbc
ruby setup.rb setup
sudo ruby setup.rb install
Test Connection from Ruby:
Heres a quick way to see if ruby can talk to SQL Server provided by Mark Imbriaco:
# irb1.8
irb(main):001:0> require "dbi"
=> true
irb(main):004:0> dbh = DBI.connect('dbi:ODBC:YOUR_DB_DEFINITION_NAME', 'USERNAME', 'PASSWORD')
=> #<DBI::DatabaseHandle:0xb7d28688 @trace_output=#<IO:0xb7d79064>, @trace_mode=2,
@handle=#<DBI::DBD::ODBC::Database:0xb7d28480 @attr={},
@handle=#<ODBC::Database:0xb7d284a8>>>
irb(main):005:0> quit
Note: if you installed RoR using MacPorts, ensure your odbc.ini and odbcinst.ini are in /opt/local/etc or else Ruby will complain that it can’t find your DSNs.
development:
adapter: sqlserver
mode: odbc
dsn: MyDSN
username: USERNAME
password: PASSWORD
Please note: should you run rake from your application root at this point you will receive failure messages along the lines of:sh: line 1: scptxfr: command not found
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21: command not found: scptxfr /s /d /I /F db /q /A /r
sh: line 1: osql: command not found
The Rake file lib/tasks/databases.rake assumes you have these two utilities installed; unfortunately they’re Windows only. See defect 3298
There is a gem for this…rubyforge.org… mssqlclientadapter
development:
adapter: sqlserver
mode: odbc
dsn: DRIVER=/opt/local/lib/libtdsodbc.so;TDS_Version=8.0;SERVER=sqlserver.com;DATABASE=DBNAME;Port=14330;uid=dbusername;pwd=dbpassword;