I am having such hard time to connect to a ODBC data source. (Windows
XP)
I have C# and ADO.NET background and I am shocked when I see there is so
little database connectivity support for Ruby!
Really? I’ve never had a problem, using MySQL, MS Access, MS SQL
Server, Oracle, and others…
Am I missing something or Ruby is not good for connecting to databases?
http://ruby-dbi.rubyforge.org/ is the home page for Ruby DBI, which
provides DB connectivity, ODBC should work for most DBs at the least I
would think (If you are on Windows and can create a DSN for the data
source, that is).
I am having such hard time to connect to a ODBC data source. (Windows
XP)
I have C# and ADO.NET background and I am shocked when I see there is so
little database connectivity support for Ruby!
Am I missing something or Ruby is not good for connecting to databases?
That is quite laughable actually. I suppose you have not heard of Ruby
on Rails and it’s ActiveRecord database framework?
In regards to your question, here is an example (a constructor for a
database dumper) using the Ruby ODBC library
(ODBC Binding for Ruby), which I believe is installed by
default in the Windows One-Click installer:
def initialize(sql, datasource = DEFAULT_DS, pw = DEFAULT_PW)
@sql, @datasource, @pw = sql, datasource, pw
@output = ''
ODBC::connect(datasource, 'sa', pw) do |dbc|
stmt = dbc.run(sql)
table = /from *(.*) */.match(sql)[1]
prefix = "INSERT INTO #{table} ("
cols = []
stmt.columns do |col|
prefix << col.name << ', '
cols << col
end
prefix[-2..-1] = ") VALUES "
stmt.each do |row|
@output << prefix << '('
row.each_with_index do |field, i|
@output << process_field(field, cols[i]) << ', '
end
@output[-2..-1] = ")\n"
end
end
end
I tested the connection, so I know it’s set up correctly and works.
If you look closely at the last bit of the error, ’ mantis’, it looks
like there is a space between the first quote and the DSN name. Don’t
know if that’s significant ?
Two things:
First, mysql-2.7.1-mswin32.gem has nothing to do with ODBC. It will
allow you to start from the default config/database.yml which references
mysql directly; by changing the login details and database in this file
you’ll get straight through. Configuring ODBC DSNs is orthogonal to
using this gem.
Alternatively, if you want to go via ODBC, there is the ODBC Adapter for
ActiveRecord/Rails, at http://odbc-rails.rubyforge.org/ .
I tested the connection, so I know it’s set up correctly and works.
If you look closely at the last bit of the error, ’ mantis’, it looks
like there is a space between the first quote and the DSN name. Don’t
know if that’s significant ?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.