Need database access in your Ruby application?  With Simba’s ODBC drivers, you can easily connect Ruby to many datasources, including: Salesforce, MongoDB, Spark, Presto and more!

This post will use the Simba Salesforce driver as an example, but the process is almost the same for any of our other drivers.

Follow this three step process to get started:

  1. Install Simba Driver
  2. Install Ruby Dependencies
  3. Connect to your Data

Install and Configure the Simba Driver

We have a free 30 day evaluation for all our drivers.  You can get an evaluation of the Salesforce driver here.

Have a look at our documentation for help installing the driver.  For Salesforce:

Next, configure a Data Source Name (DSN), again see our excellent documentation:

On Windows, test your DSN using the Test button in the configuration screen:

sf_test

Or see here for OSX or Linux.

Install Ruby Dependencies

If you don’t have Ruby itself installed, head on over to the Ruby page.

After successfully installing Ruby, you’ll need ruby-dbi, including the dbd-odbc gem and ruby-odbc.

gem install dbi
gem install dbd-odbc
gem install ruby-odbc

Connect to your Data

Now, it’s time for the fun part!

First, create a connection to Salesforce using your DSN.  “Simba Salesforce” is the name of the DSN.  Use your username and password.

require 'DBI'
dbi_conn = DBI.connect('DBI:ODBC:Simba Salesforce','<your username>','<your password>')

Next, execute a query and find out how many columns are in the result set:

sth = dbi_conn.execute("SELECT Name, Type, Phone, Website FROM Account LIMIT 3")
colCount = sth.column_names.size

Then, print the column names:

colNames = ''
sth.column_names.each do |name|
colNames.concat(name)
colNames.concat("\t")
end
puts colNames

Now, fetch and print the results:

while row = sth.fetch do
rowValues = ''
(0..colCount - 1).each do |n|
val = row[n]
if val.nil?
val = '<<NULL>>'
end
rowValues.concat(val)
rowValues.concat("\t")
end
puts rowValues
end
sth.finish

And finally, disconnect from the database:

dbi_conn.disconnect if dbi_conn

If your Ruby file was called SalesforceSelect.rb, you could run it from the command line like:

ruby -W0 SalesforceSelect.rb

sf_results

the -W0 flag suppresses warnings that show up from DBI.  Suppressing warnings isn’t recommended for production usage.

For more information on using ODBC in Ruby see:

For information on DBI with Ruby on Rails, have a look at: