【问题标题】:Rails select query to MSSQL returns -1:FixnumRails 对 MSSQL 的选择查询返回 -1:Fixnum
【发布时间】:2013-06-12 19:18:50
【问题描述】:

我已将 Rails 连接到外部 MSSQL 数据库。在 database.yml 中,适配器是 sqlserver。

查询代码:

class External < ActiveRecord::Base
   def self.select_all_entries
      external_connection_hash = configurations["external_DB"]
      establish_connection external_connection_hash
      con = connection()

      side = con.execute("Select * from dbo.BBOrders").fetch_row;
      return side
   end
end

我得到错误 - 未定义的方法“fetch_row” for -1:Fixnum

数据库信息正确。我试图访问另一个表并得到相同的答案,但是如果我访问一个没有条目的表,它会引发另一个异常,所以它正在连接但没有给出值。有什么想法吗?

编辑 database.yml

development:
  adapter: sqlserver
  dsn: K_Connection
  #dsn: mydsn
  mode: odbc
  #mode: dblib
  encoding: utf8
  database: KTrade
  host: xxx.xxx.xxx.xxx
  username: tr
  password: tr2

【问题讨论】:

  • 这是您应用的主数据库吗?或者这是您尝试建立连接的辅助数据库?
  • 这是我的主数据库。但这会如何改变呢?

标签: ruby-on-rails database ruby-on-rails-3 sql-server-2008


【解决方案1】:

您无需设置 Model 类即可建立数据库连接。

在database.yml中定义你的数据库,去掉External类,ActiveRecord::Base会在你加载rails时建立连接(无论是rails s还是rails c)

在 database.yml 中,您告诉活动记录您正在使用 sqlserver 适配器,这意味着您的 Gemfile 中应该有这个 gem:https://github.com/rails-sqlserver/activerecord-sqlserver-adapter

在 Rails 中定义模型类的惯例是将其定义为资源。您有一个名为 dbo.BBorders 的表名。有点奇怪的名字,它可能应该只是“bb_orders”或“orders”。那么您的型号名称是:

class BbOrder < ActiveRecord::Base
  #pay attention to capitalization and singularity/plurality
  #convention is for table name be the plural, camel cased version of the model name
  #BbOrder is the model name. bb_orders is the table name
end

class Order < ActiveRecord::Base
end

如果您的表名必须是“dbo.BBOrders”,那么您可以覆盖 Rails 期望的默认表名:

class BbOrder < ActiveRecord::Base
  self.table_name = "dbo.BBOrders"
end

一旦您定义了模型并与表关联,您就可以从控制台查询它:

BbOrder.all

ActiveRecord will generate the query:
"select * from bb_orders" (or whatever the table name is)

如果你真的想编写自定义查询,你可以:

BbOrder.connection.execute("<whatever arbitrary sql you want")

如果您尝试“获取一行”,那么您必须向它提供有关您尝试获取的行的一些信息:

BbOrder.find(45)
will generate:
"select bb_orders.* from bb_orders where id = 45"
and this will return an instance of your BbOrder class.

或更复杂的东西:

BbOrder.where(:shipped => true)
will generate:
"select bb_orders.* from bb_orders where shipped"
and this will return collection of BbOrder instances where the attribute shipped is true
in other words, a row in the database where the shipped column is true 
corresponds to one BbOrder instance

【讨论】:

  • 你有没有从中得到帮助?
  • 我不明白这个命名约定。我的表必须命名为 BBOrders。那么这是否意味着我不能使用导轨?我是否在模型文件夹中创建一个 bb_orders.rb 并复制该类?
  • 您的代码假定我正在连接到默认的 sqlite 数据库。我有一个外部的。您将如何将其与您的代码联系起来?
  • 你仍然可以使用 rails,将你的模型类命名为 BbOrders
  • 我做到了,我拥有了一切。我认为我没有朝正确的方向挖掘。当我尝试进行查询时,它显示我正在使用我的数据库,但是当我查询它时,它显示无效的对象名称“订单”。我仅出于测试目的在数据库中创建了一个订单表,并遵循了命名约定。但它仍然不会查询。
【解决方案2】:

我认为你需要一颗宝石。 DBI ODBC Drive 可能是众多可用选项之一。但是请尝试此 URL (http://www.easysoft.com/developer/languages/ruby/rails.html#introduction) 上的说明

最终结果将如下所示

require 'dbi' # Replace MY_DSN with the name of your ODBC data # source. Replace and dbusername with dbpassword with # your database login name and password. DBI.connect('dbi:ODBC:MY_DSN', 'dbusername', 'dbpassword') do | dbh | # Replace mytable with the name of a table in your database. dbh.select_all('select * from mytable') do | row | p row end end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多