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. While TinyTDS can be installed using the gem install command, the version that is installed by default does not currently support connectivity to SQL Database (it will work fine for SQL Server.) In order to communicate with SQL Database, TinyTDS requires a version of FreeTDS that has been compiled with OpenSSL support. While providing a version of this via gem install is on the TODO list (https://github.com/rails-sqlserver/tiny_tds,) you can currently compile your own version of FreeTDS to enable SQL Database connectivity.
NETWORK_LIBS="$NETWORK_LIBS –lssl –lcrypto"
NETWORK_LIBS="$NETWORK_LIBS –lssl32 –leay32 –lwsock32 –lgdi32"
./configure –prefix=<path to install FreeTDS to> —with-libiconv-prefix=<path to iconv> —with-openssl=<path to ssl>
./configure –prefix=/c/freetds –with-libiconv-prefix=/c/libiconv –with-openssl=/c/openssl
gem install tiny_tds -platform=ruby -- --with-freetds-dir=<FreeTDS installation folder>
typedef tds_sysdep_int64_type DBBIGINT;
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}" }
Larry Franks edited Revision 10. Comment: Updating with a needed line for database.yml, also calling out that only older versions of Rails/ActiveRecord/activerecord-sqlserver-adapter will need you to manually create a clustered index. New versions should 'just work'.
patmas57 edited Revision 8. Comment: Branding update
Ed Price - MSFT edited Revision 4. Comment: Spacing and tags
Larry Franks edited Revision 2. Comment: fixing links
Larry Franks edited Revision 1. Comment: adding TOC
Larry Franks edited Original. Comment: adding odata info I forgot first time around.
Interesting article. However in my experience, it is much much easier to use jruby and the JDBC SQL server driver.
Just download the jdbc jar (sqljdbc4.jar), the activerecord jdbc adapter (activerecord-jdbc-adapter) and then use the following configuration in your database.yml:
azureprod:
adapter: "jdbc"
username: "username"
password: "your_password"
driver: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url: "jdbc:sqlserver://YourDatabaseEndpoint.database.windows.net:1433;databaseName=DatabaseNameDB"
cheers,
thomas