English 中文(简体)
如何选择一个工作日与sqlite/sqequuel?
原标题:How to select mondays with sqlite/sequel?

在我的 SQLite- DB 中,我想在星期一选择所有条目 。

我怎么能用塞克尔呢?

试验示例:

require  sequel 

DB = Sequel.sqlite()# test.db )
DB.create_table(:days) do
    String :text
    Date  :start
end

Date.today.upto(Date.today + 30){|d|
  DB[:days].insert( :text => d.strftime("%Y-%m-%d day %w in week %W"), :start => d)
}

我怎么能选择每个星期一?

在本地" https://stackoverflow.com/ questions/602245/how-can-i-select-entries-for-a-given-day-using-sql"中, SQL I can do :

select * from days where strftime("%w", start) = "1"

使用此选项, 我可以定义一个视图, 并为此选择 :

DB.run( create view mondays as select * from days where strftime("%w", start) = "1" )
p DB[:mondays].all

但我想用塞克尔的

我试过了

sel = DB[:days].filter{ start.strftime("%w", :start) ==  1  }  
#NoMethodError

sel = DB[:days].filter{ Sequel::SQL::Function.new(:strftime, "%w", :start) ==  1  }
#SELECT * FROM `days` WHERE (1 = 0)

但没有成功。

还有其他解决办法吗?

我还在寻找按时选择项目的可能性(所有项目都有一个时间戳,然后从中午12:00至13:00),我认为这是同样的问题,周选一日的解决方案也将解决我的另一个问题。

最佳回答

以下是你举的例子中的问题:

sel = DB[:days].filter{ start.strftime("%w", :start) ==  1  }  
#NoMethodError

这使用了一个虚拟行, 所以调用 start 返回一个 sequel:: SQL::: Ididation . sequel::: SQL::: Idacter#strftime 方法不存在, 因此不存在 NoMethodError 。

sel = DB[:days].filter{ Sequel::SQL::Function.new(:strftime, "%w", :start) ==  1  }
#SELECT * FROM `days` WHERE (1 = 0)

由于您正在使用 失败。 Sequel 不覆盖 和 ruby S 默认 返回 false 。 在 SQLite 上, Sequel 代表 false , 作为 (1 = 0)

这项工作应:

sel = DB[:days].filter{{strftime("%w", :start) =>  1 }}
# SELECT * FROM `days` WHERE (strftime( %w , `start`) =  1 )

在Sequel, 平等由hashes代表。

问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...