Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby connecting to SQL Server

I'm having troubles connecting Ruby to Microsoft SQL Server. I'm running Mac OS X, but the target environment is Ubuntu Linux.

Here's what I've tried:

  • Install unixODBC
  • Install FreeTDS
    • used the options --with-unixodbc=/usr/local/etc --with-tdsver=8.0

I then had these files in /usr/local/etc:

  • odbc.ini
  • odbcinst.ini
  • freetds.conf

I added a reference to the FreeTDS driver in the odbcinst.ini file to my ODBC driver file like this:

;
; odbcinst.ini
;
;
[FreeTDS]
Driver = /usr/local/lib/libtdsodbc.so

Then I configured the server in the freetds.conf file like this:

# Aries database server (SQL Server 2008)
[aries-db1]
        host = xx.xx.xx.xx
        port = 1433
        tds version = 8.0

And finally I added the ODBC DSN in the odbc.ini file like this:

[aries-db1]
Driver      =   FreeTDS
Description =   ODBC Connection via FreeTDS
Trace       =   1
Servername  =   aries-db1
Database    =   MY_DB
UID         =   user1
PWD         =   pass1

I can verify that my server is online and the port is open (via telnet & yougetsignal.com port check).

As a test, I did this:

tsql -S aries-db1 -U user1 -P pass1

And it seemed to connect just fine. Passing in invalid values resulted in expected errors.

So finally to my question:

How do I extend this to Ruby? Nothing I've tried so far has worked. I tried Sequel like this:

require 'sequel'
Sequel.connect('aries-db1')['select * from foo'].all

And I get an error like this:

Sequel::DatabaseConnectionError: ODBC::Error: S1000 (0) [unixODBC][FreeTDS][SQL Server]Unable to connect to data source

Which tells me it is finding my driver configuration correctly, but for some reason cannot connect.

I also tried DBI like this:

DBI.connect('DBI:ODBC:aries-db1')

And I get a similar error.

Any suggestions? I feel like I'm very close, but am not sure what to try next to troubleshoot this.

like image 825
Ben Scheirman Avatar asked Jan 13 '11 04:01

Ben Scheirman


People also ask

Can Ruby connect to database?

In Ruby we can connect to database using DBI module. DBI module provides Database Independent Interface to access the data source. So, using DBI, the underlying data source can be any database like Oracle, MySQL, Postgres etc, however the API remains the same.

How do I connect to a SQL Server server?

Start the SQL Server, in the dialog window for the Server name enters the name of the instance that you want to connect with. From the Authentication drop down box, select the SQL Server Authentication and for the field Login and the Password enter your credentials then click the Connect button.

Does Ruby use SQL?

In SQLite Ruby module, first we prepare the SQL statement with the prepare method. The SQL string is sent to the database engine, which checks the statement validity, syntax and in some databases also the user permissions to perform certain queries. If all is OK, a statement object is returned to the Ruby script.

What is TinyTDS?

TinyTDS - A modern, simple and fast FreeTDS library for Ruby using DB-Library. Developed for the ActiveRecord SQL Server adapter.


1 Answers

Have a Look at https://github.com/rails-sqlserver/tiny_tds TinyTds - A modern, simple and fast FreeTDS library for Ruby using DB-Library

Its easy to install and easy to use

like image 135
Klaus Avatar answered Oct 04 '22 07:10

Klaus