From: Ryan Leavengood Date: 2006-05-02T05:39:20+09:00 Subject: Re: ODBC Database Connectivity On 5/1/06, Alan wrote: > Hi, > > 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 (http://www.ch-werner.de/rubyodbc/), 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 Ryan