Click here to change the language (ja-JP). This article discusses the methods of connecting to Windows Azure SQL Database from the Ruby language. While this article discusses several gems that can be used to connect to SQL Database, it is by no means a comprehensive listing of all gems that provide this functionality.
For connectivity to SQL Database, we will be using the Ruby ODBC, TinyTDS, and Ruby OData gems. While there are other database connectivity methods available for the Ruby language, not all provide connectivity to SQL Database.
All three gems can be installed through the gem command (http://docs.rubygems.org/read/book/2) by issuing the following commands:
gem install ruby-odbc gem install tiny_tds gem install ruby_odata
Ruby ODBC provides access to ODBC data sources, including SQL Database. This gem relies on your systems ODBC drivers for connectivity, so you must first ensure you have a working ODBC connection to your SQL Database. Perform the following steps to configure and test an ODBC connection:
The following code is an example of using Ruby ODBC to connect to a SQL Database specified by a DSN named ‘azure’, perform a select against the ‘names’ table, and return the value of the ‘fname’ field.
require 'odbc' sql='select * from [names]' datasource='azure' username='user@servername.database.windows.net' password='password' ODBC.connect(datasource,username,password) do |dbc| results = dbc.run(sql) results.fetch_hash do |row| puts row['fname'] end end
Active Record can also use Ruby ODBC to connect to a SQL Database. The following is an example database.yml for using an ODBC connection and the activerecord-sqlserver-adapter gem.
development: adapter: sqlserver mode: ODBC dsn: Driver={SQL Server};Server=servername.database.windows.net;Uid=user@servername.database.windows.net;Pwd=password;Database=databasenameazure: true
CREATE CLUSTERED INDEX [idx_schema_migrations_version] ON [schema_migrations] ([version])
After creating the clustered index, rerun the migration and it should succeed.
TinyTDS does not rely on ODBC to connect to SQL Database; instead it directly uses the FreeTDS library. TinyTDS is probably the fastest of the methods to connect to SQL Database from Ruby, and is easy to use; just install it and give it the connection string. It is also cross-platform, so you can use it on Windows, OS X, Linux, etc. Note: Previous versions were a bit of a complicated install, as you had to build OpenSSL, then build FreeTDS against that, then TinyTDS against that. But with the latest version you can download and install a version that takes care of all that for you.
Use the following command to install the pre-compiled version of TinyTDS, which includes support for securely connecting to SQL Database:
gem install tiny_tds --pre
This will install the precompiled 0.6.3-rc2 version (as of August 5th, 2015.) If you want to specify the version in a Gemfile, use:
gem "tiny_tds", "0.6.3-rc2"
The following code is an example of connecting to SQL Database using the tiny_tds gem:
require 'tiny_tds' client=TinyTds::Client.new(:username=>’user’, :password=> ‘password’, :dataserver=>’servername.database.windows.net', :port=>1433, :database=>’databasename’, :azure=>true) results=client.execute("select * from [names]") results.each do |row| puts row end
Tiny_tds can also be used with ActiveRecord. The following is an example database.yml for using a dblib connection to SQL Database using the tiny_tds gem.
development: adapter: sqlserver mode: dblib dataserver: 'servername.database.windows.net' database: databasename username: user password: password timeout: 5000 azure: true
The Ruby OData gem allows you to connect to an OData service. OData is a RESTful method of accessing data over the internet, using standards such as JSON, AtomPub, and HTTP. For more information on OData, see http://www.odata.org/.
The following code will connect to an OData service that contains a database named 'testdb'. The code will then select rows from a table named 'People', and will display the contents of the 'fname' and 'email' fields.
require 'ruby_odata' svc=OData::Service.new "https://odata.sqlazurelabs.com/OData.svc/v0.1/servername/testdb" svc.People people=svc.execute people.each {|person| puts "#{person.fname} can be contacted at #{person.email}" }