user3541952
user3541952

Reputation: 11

Ruby SQL Connection Issue

I'm trying to write a standalone script so I can run a regular cron job that will update the database as well as cache some data to a file locally so we don't have to wait for query times. In doing so, I am using ActiveRecord. I have the following code:

require "active_record"
require "rubygems"
require "./lib/queries/my_query_file"

def my_method

    #get stored query string in my_query_file
    sql = MY_QUERY

    @sql_con = ActiveRecord::Base.establish_connection(
       :adapter => "sqlserver",
       :host => "my_host",
       :port => "my_port",
       :username => "my_user",
       :password => "my_pass",
       :database => "my_db",
       :timeout => "100000"
    )

    @@query_result = @sql_con.connection.select_all(sql)

    @@query_result.each do |row|
        #do something
    end
end

When I try to run the above script I get the following error:

Specified 'sqlserver' for database adapter, but the gem is not loaded. Add gem '' to your Gemfile. (Gem::LoadError)

Any idea on what the issue could be? I've exhausted my search options to the point where I've gotten a headache from searching for answers. I finally caved in to post a question to see if there are any experts that help or folks that have encountered this issue before that might recall the solution.

Upvotes: 0

Views: 89

Answers (2)

user3541952
user3541952

Reputation: 11

That was kinda lame. I tried what user944938 suggested and that worked. I was just hoping that I can get it to work with ActiveRecord since that is what I am using elsewhere. I updated my code to look like this now:

require "tiny_tds" require "./lib/queries/my_query_file"

def my_method

    sql = MY_QUERY

    client = TinyTds::Client.new(
       :username => "my_user", 
       :password => "my_pass", 
       :host => "my_host", 
       :database => "my_db"
    )

    @@build_query = client.execute(sql)

    @@build_query.each do |row|
       #do something
    end

end

Upvotes: 0

user944938
user944938

Reputation: 1020

You are using :adapter => "sqlserver", which makes ruby assume that sqlserver is the database which you are trying to use. It trying to make a lookup for a ruby gem which has adapter connection for sqlserver.

When we use mysql gem, we can see there are library extensions written in C which help us connect over the ports to current mysql server.

Upvotes: 1

Related Questions