首页 > 解决方案 > 如何使用建立连接创建类以建立数据库连接 - RUBY

问题描述

我对Ruby语言了解不多。我正在尝试创建一个类来建立 oracle 数据库连接。此类将接收要在其他类中重用的连接。我正在从事一个自动化项目,需要建立这种连接,以便为自动化测试准备大量数据。下面我将提供我的代码,以便有人可以指导我如何创建这个类。在这种情况下,无法访问连接类。

require "rubygems"
require "active_record"

class Connection
    attr_accessor :connection

  ActiveRecord::Base.establish_connection(
    :adapter => "oracle_enhanced",
    :database => "dboraclehomolog.com.br:1521/homolog",
    :username => "USER",
    :password => "password",
  )

  def initialize
    connection = ActiveRecord::Base.connection
  end

  def execute_select(sql)
    results = ActiveRecord::Base.connection.execute(sql)

    if results.nil?
      return results
    else
      return false
    end
  end

  def exec(sql)
    begin
      ActiveRecord::Base.connection.execute(sql)
    rescue => e
      puts e.message
    end
  end
end

错误:未初始化的常量 Repactuar::Conexao (NameError)

标签: rubyactiverecordrails-activerecord

解决方案


推荐阅读