0

我正在使用 ruby​​ 助手从使用sequel ORM 的 SQLite3 数据库中提取数据。这是代码:

#!/usr/bin/env ruby
# encoding: UTF-8

require_relative 'greekdate'
require 'sequel'

module Pharmacy
    class Open
        def initialize
            db = Sequel.sqlite("../lib/drama.sql")
            @address = db[:addressbook]
            @ov = db[:overnight]
            @grday = GRDay::MDate.new
        end                         

        def display
            data = @address.first(id: get_id()) # ERROR HERE
            p data[:name]
        end

        private
        def get_id
            mod_date = @grday.get[:mod_date]
            @ov.each do |entry|
                return entry[:pharmacy_id] if entry[:date].to_s == mod_date
            end
        end
    end
end

我正在调用该display方法。我得到的错误是:

/Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize': SQLite3::SQLException: only a single result allowed for a SELECT that is part of an expression (Sequel::DatabaseError)
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:263:in `query'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/adapters/sqlite.rb:179:in `block (2 levels) in _execute'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/database/logging.rb:33:in `log_yield'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/adapters/sqlite.rb:179:in `block in _execute'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/database/connecting.rb:229:in `block in synchronize'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/database/connecting.rb:229:in `synchronize'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/adapters/sqlite.rb:172:in `_execute'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/adapters/sqlite.rb:122:in `execute'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/dataset/actions.rb:794:in `execute'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/adapters/sqlite.rb:356:in `fetch_rows'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/dataset/actions.rb:144:in `each'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/dataset/actions.rb:584:in `single_record'
    from /Users/atma/.rvm/gems/ruby-1.9.3-p194/gems/sequel-4.0.0/lib/sequel/dataset/actions.rb:202:in `first'
    from test.rb:17:in `display'
    from test.rb:32:in `<main>'

我不确定这是为什么。起初我以为我应该以某种方式关闭并重新打开连接,但错误说“只有一个结果允许作为表达式一部分的 SELECT ”,直到昨天我才得到一个结果,我看不出任何理由因为我正在调用表格,所以我得到了 2 个结果。第一个(id:X)

任何想法和解释都将受到欢迎:-)

谢谢你的时间,

功放

4

1 回答 1

1

如果 in 中没有给定日期的条目,则您的get_id方法将返回,因为这是 的返回值。你可能想要这样的东西:@ov@ov@ov.each

def display
    if id = get_id
      data = @address.first(id: id)
      p data[:name]
    end
end

private
def get_id
    mod_date = @grday.get[:mod_date]
    @ov.each do |entry|
        return entry[:pharmacy_id] if entry[:date].to_s == mod_date
    end
    nil
end

请注意,display对于方法名称,这是一个糟糕的选择,因为Object#displayruby​​ 已经定义了它。

于 2013-07-05T16:04:19.747 回答