我正在使用一个ruby助手,使用续集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只允许一个结果",直到昨天我才得到一个结果,我认为没有理由得到两个结果,因为我正在调用表第一个(id:X)。
任何想法和解释都非常受欢迎:-)
感谢您抽出时间,
PA
如果@ov
中没有指定日期的条目,则get_id
方法返回@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定义了。