Skip to main content

How to connect to CSV files using Ruby DBI ODBC and no DSN

I had a hard time finding a way of connecting to csv files using ruby's dbi through the odbc driver without using a DSN. The odbc turned not to work with Microsoft Text driver for odbc because the name of the driver has a semicolon in it. Thus, the ruby code fro the driver was splitting the name of driver, resulting in a keyword syntax error. The following is the (working) code that shows how to connect to csv files using dbi and odbc, note that I had to override require to add the fix to the odbc driver because dbi loads the driver using require:
require "dbi"   

alias :old_require :require 

def require(library)
  old_require(library)
  if library.downcase.include?("odbc")
    define_odbc_fix()
  end
end

def define_odbc_fix
  DBI.class_eval <<-'THERE'
 
  module DBD
    module ODBC
      class Driver < DBI::BaseDriver
        #JPT: fixed to allow the ";" character in the connection string.
        # Obeys the rule of "If a semicolon (;) is part of a value it must be delimited by quotation marks (")"
        # quoted from http://connectionstrings.com/default.aspx?article=what_rules_apply_to_connection_strings
        def connect(dbname, user, auth, attr)
    
        colon_stuffing_string = "@WACKYsUFF@"
          dbname.gsub!('";"', colon_stuffing_string)
          driver_attrs = dbname.split(';')

          if driver_attrs.size > 1
            # DNS-less connection
            drv = ::ODBC::Driver.new
            drv.name = 'Driver1'
            driver_attrs.each do |param|
              param.gsub!(colon_stuffing_string, ";")
              pv = param.split('=')
              next if pv.size < 2
              drv.attrs[pv[0]] = pv[1]
            end
            db = ::ODBC::Database.new
            handle = db.drvconnect(drv)
          else
            # DNS given
            handle = ::ODBC.connect(dbname, user, auth)
          end

          return Database.new(handle, attr)
        rescue ODBCErr => err
          raise DBI::DatabaseError.new(err.message)
        end
      end
    end
  end
  THERE

end

begin
  conn_str = "DBI:ODBC:Driver={Microsoft Text Driver (*.txt\";\" *.csv)};Dbq=$$path_to_folder_with_csvs$$;Extensions=csv;"
  conn_str.gsub!('$$path_to_folder_with_csvs$$', File.expand_path(".\\spreadsheets\\simple"))
  dbh = DBI.connect(conn_str, "", "")
  
  row = dbh.select_one("SELECT * from this_year_trancripts.csv")
  puts row.to_s
rescue DBI::DatabaseError => e
  puts "An error occurred"
  puts "Error code: #{e.err}"
  puts "Error message: #{e.errstr}"
ensure
  # disconnect from server
  dbh.disconnect if dbh
end

Comments

Popular posts from this blog

Powershell script for converting JPG to TIFF

The following Powershell script will convert a batch of JPEG files to TIFF format: #This Code is released under MIT license [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $files_folder = 'C:\path-where-your-jpg-files-are\' $pdfs = get-childitem $files_folder -recurse | where {$_.Extension -match "jpg"} foreach($pdf in $pdfs) { $picture = [System.Drawing.Bitmap]::FromFile( $pdf.FullName ) $tiff = $pdf.FullName.replace('.PDF','').replace('.pdf','').replace('.jpg','').replace('.JPG','') + '.tiff' $picture.Save($tiff) }

Power Automate: SFTP action "Test connection failed"

When I added an SFTP create file action to my Power Automate flow ( https://flow.microsoft.com ) , I got the following error in the action step, within the designer: "Test connection failed" To troubleshoot the Power Automate connection, I had to: go the Power Automate portal then "Data"->"Connections"  the sftp connection was there, I clicked on the ellipsis, and entered the connection info It turns out, that screen provides more details about the connection error. In my case, it was complaining that "SSH host key finger-print xxx format is not supported. It must be in 'MD5' format". I had provided the sha fingerprint that WinScp shows. Instead, I needed to use the MD5 version of the fingerprint. To get that, I had to run in command line (I was in a folder that had openssh in it): ssh -o FingerprintHash=md5 mysftpsite.com To get the fingerprint in MD5 format. I took the string (without the "MD5:" part of the string) and put

Alert if file missing using Powershell

The following Powershell script can be used to send an email alert when a file is missing from a folder or it is the same file from a previous check: $path_mask = "yourfile_*.txt" $previous_file_store = "lastfileread.txt" $script_name = "File Check" ###### Functions ########## Function EMailLog($subject, $message) {    $emailTo = "juanito@yourserver.com"    $emailFrom = "alert@yourserver.com"    $smtpserver="smtp.yourserver.com"       $smtp=new-object Net.Mail.SmtpClient($smtpServer)    $smtp.Send($emailFrom, $emailTo, $subject, $message) } Try {    #get files that match the mask    $curr_file = dir $path_mask |  select name    if ($curr_file.count -gt 0)    {        #file found        #check if the file is different from the previous file read        $previous_file = Get-Content $previous_file_store        $curr_file_name = $curr_file.Item(0).Name        if ($