Ruby Sequel(SQlite3)“选择”错误
我正在使用ruby助手使用ORM从SQLite3数据库提取数据。代码如下:Ruby Sequel(SQlite3)“选择”错误,ruby,sqlite,sequel,Ruby,Sqlite,Sequel,我正在使用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
#!/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只允许一个结果,直到昨天我才得到一个结果,我认为没有理由得到两个结果,因为我调用了table.firstid: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是一个糟糕的选择,因为Objectdisplay已经由ruby定义。您应该把代码放在问题中,而不是链接。它会持续很长时间。至于您的问题,我相信如果在get_id中找不到匹配项,那么该方法将返回查询db[:隔夜],因为这是每个方法的行为。无论哪种方式,这都可能是一个bug,并可能导致您的问题。通常,您会在查询中添加.where子句,而不是在Ruby中实现搜索。你能在问题中加入你的代码吗?嗨,尼尔,我把代码放在了问题中,不过你是对的。我使用了2个SQLite3文件,一个用于测试,一个用于生产,我把它们混在一起,结果是空记录。我只想提出一个例外,数据库是空的,没有id返回,我会没事的!谢谢没错,为了避免愚蠢的冲突,我换了那个通宵显示。我添加了一个rescue子句,以便在没有匹配项的情况下得到一个优雅的错误和可理解的错误,这无论如何都不会发生。