0

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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Does require "./lib/queries/my_query_file" have any connection adapter code for you sqlserver ?
it's basically a variable that holds the entire SQL query. The query is quite long and elaborate, so I figured it would be cleaner to save that in a different file and just get the string loaded into a variable
Does that mean I need to remove the line :adapter => "sqlserver"? If so, how would it know what kind of adapter to use?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.