我正在使用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个结果,因为我正在调用表格。 first(id:X)。
任何想法和解释都非常受欢迎: - )
感谢您的时间,
PA
答案 0 :(得分:1)
如果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#display
已由ruby定义。