What is the best way to connect to SQL Server from Ruby on OS X?

I need the Rails application to connect to the MS SQL Server database and make a simple search query. The main database for the application is MySQL. It just needs to do this thing SQL Server on the side.

What is the best way to do this?

I could write a completely separate Java application that connects to the database and uploads some XML data to the file system for my Ruby application.

Or I could team up with a Ruby ODBC connection. I did a search in the Gemcutter and found them ...

  • dbd-odbc (0.2.5) 11141 downloads
  • ruby-odbc (0.99992) 6390 downloads
  • activeerecord-odbc-adapter (2.0) 2333 download
  • odbc-rails (1.5) 2167 downloads

This would mean connecting to two different databases from the same Rails application. I don’t even know how to do this.

- SQL Server Ruby? , ?


UPDATE -

. ​​ , . . . .

, SQL Server Ruby OS X

, , FreeTDS

, . , .

/usr/local/freetds/lib.

. .

, : " tsql FreeTDS ".

/etc/profile:

# 2010-10-19
# To support the FreeTDS library for connecting Ruby to SQL Server.

PATH=$PATH:/usr/local/freetds/bin

# Have FreeTDS to log some output.
#export TDSDUMP=/tmp/freetds.log
#export TDSDUMPCONFIG=/tmp/freetdb_config.log

export PATH

FreeTDS

FreeTDS. , . , , ~/.freetds.conf...

[DATA_SERVER_NAME]
    host = hostname
    port = 1433
    tds version = 8.0

. [DATA_SERVER_NAME] , . . Ruby- tiny_tds gem.

ODBC

, FreeTDS, odbc.ini odbcinst.ini. .

tiny_tds

tiny_tds Ruby FreeTDS SQL Server. tiny_tds ...

'... , FreeTDS libiconv , . "tsql -C" "iconv library: yes". '

, .

:

require 'tiny_tds'
client = TinyTds::Client.new(:username => '...username...', :password => '...password...', :dataserver => 'DATA_SERVER_NAME')
sql = '... whatever ...'
result = client.execute(sql)
client.close

ActiveRecord

ActiveRecord SQL Server , , , activerecord-sqlserver-adapter. , .

+3
4

MS SQL.

:

+5

, , ODBC database.yml. , :

class SQLServerThing < ActiveRecord::Base
  establish_connection :sqlserver
  set_table_name 'things'
end

:

SQLServerThing.find(1)

, :

SQLServerThing.connection.select_all("SELECT * FROM table ...")

ActiveRecord, .

(magic_multi_connections) , . , .

+1
0

Here is a good tutorial on how to do this.

In general, you will need:

System level

  • Freetds
  • tdsodbc
  • libdbd-ODBC-ruby

Gems

  • Dbi
  • DBD-ODBC
  • ActiveRecord-SQLServer adapter
  • ActiveRecord-ODBC adapter

The last two are if you plan to use activerecord.

0
source

Source: https://habr.com/ru/post/1770510/


All Articles