无法输出包含 Sequel 中日期的 MySQL 表
我正在尝试使用 Sequel 来访问 Ruby 中的 MySQL 数据库。当我尝试访问涉及日期列的表时,出现错误。当我访问没有的表时,它运行良好。怎么了?
示例代码:
require 'rubygems'
require 'sequel'
DB = Sequel.connect(:adapter=>'mysql', :host=>'localhost', :database=>'db', :user=>'user', :password=>'password')
event = DB[:table]
puts event.all
错误:
/usr/lib/ruby/1.8/date.rb:956:in `new_by_frags': ArgumentError: invalid date (Sequel::InvalidValue)
访问不包含日期的表时,不会显示该错误。这是在 Debian 上运行的。
I'm trying to use Sequel to access a MySQL database in Ruby. When I try accessing a table which involves a date column, I am presented with an error. When I access a table without, it functions fine. What is wrong?
Example Code:
require 'rubygems'
require 'sequel'
DB = Sequel.connect(:adapter=>'mysql', :host=>'localhost', :database=>'db', :user=>'user', :password=>'password')
event = DB[:table]
puts event.all
Error:
/usr/lib/ruby/1.8/date.rb:956:in `new_by_frags': ArgumentError: invalid date (Sequel::InvalidValue)
The error is not shown when a table which does not feature a date is accessed. This is running on Debian.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:
不再有效,但我刚刚通过在数据库对象上设置选项解决了同样的问题:
希望这对其他人有帮助!
Heads up:
no longer works, but I just fixed this same problem by setting the option on my DB object:
Hope this helps someone else!
我也有同样的问题。这是由 MySQL 的零日期“0000-00-00”上的 Sequel 阻塞引起的。我使用的解决方案是设置
(在这里找到:http://groups. google.com/group/sequel-talk/browse_thread/thread/152a4131bd280966)。
如果您控制数据库,您还可以使用 NO_ZERO_DATE SQL 模式阻止 MySQL 存储零日期: http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html#sqlmode_no_zero_date。
I had the same problem. It was caused by Sequel choking on MySQL's zero date ‘0000-00-00’. The solution I used was to set
(found here: http://groups.google.com/group/sequel-talk/browse_thread/thread/152a4131bd280966).
If you control the DB, you could also prevent MySQL storing zero dates using the NO_ZERO_DATE SQL mode: http://dev.mysql.com/doc/refman/5.1/en/server-sql-mode.html#sqlmode_no_zero_date.
最终的解决方案涉及改用 data_objects Ruby gem。这避免了使用本机 C MySQL 驱动程序时出现的问题。
代码调整如下:
这可能会导致性能问题,但这超出了我最初问题的范围。
感谢 lexu 对原始问题的评论。
The final solution involved switching to use the data_objects Ruby gem. This avoided issues using the native C MySQL driver.
The code is adjusted as follows:
Possibly this could cause performance issues, but this is beyond the scope of my initial issue.
Thanks to lexu for commenting on the original question.