Rails 3 execute custom sql query without a model

后端 未结 5 1456
鱼传尺愫
鱼传尺愫 2020-11-27 12:20

I need to write a standalone ruby script that is supposed to deal with database. I used code given below in rails 3

@connection = ActiveRecord::Base.establis         


        
5条回答
  •  爱一瞬间的悲伤
    2020-11-27 13:01

    How about this :

    @client = TinyTds::Client.new(
          :adapter => 'mysql2',
          :host => 'host',
          :database => 'siteconfig_development',
          :username => 'username',
          :password => 'password'
    
    sql = "SELECT * FROM users"
    
    result = @client.execute(sql)
    
    results.each do |row|
    puts row[0]
    end
    

    You need to have TinyTds gem installed, since you didn't specify it in your question I didn't use Active Record

提交回复
热议问题