2013-07-05 27 views
0

我正在使用ruby幫助程序,使用sequel ORM從SQLite3數據庫提取數據。這裏是代碼:Ruby續集(SQlite3)'SELECT'錯誤

#!/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

+2

你應該把你的代碼放在問題中,而不是鏈接。它會保持更長的時間。至於你的問題,我相信如果在get_id()中找不到匹配,那麼該方法將返回查詢'db [:overnight]'(因爲這是'each'的行爲)。這可能是一個錯誤,無論哪種方式,並可能導致您的問題 –

+1

通常你會爲查詢添加'.where'子句,而不是在Ruby中實現搜索。你能否在你的問題中包括你的代碼? –

+0

嗨,尼爾,我把代碼放在你說得對的問題上。我使用了2個SQLite3文件,一個用於測試,另一個用於生產,我將它們混合到空記錄結束。我只是把一個例外,不是數據庫是空的(沒有身份證返回),我會沒事的! 謝謝! –

回答

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已經被定義紅寶石。

+0

的確,我改變了這個隔夜視角,以避免愚蠢的衝突。我添加了一個救援條款,以便在沒有條目匹配的情況下獲得優雅的錯誤和可理解的錯誤(不應該發生這種情況)。 –