无法输出包含 Sequel 中日期的 MySQL 表

发布于 2024-08-09 16:55:24 字数 512 浏览 10 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-08-16 16:55:24

注意:

Sequel::MySQL.convert_invalid_date_time = nil

不再有效,但我刚刚通过在数据库对象上设置选项解决了同样的问题:

DB = Sequel.connect("mysql://localhost/#{DB_NAME}")
DB.convert_invalid_date_time = nil

希望这对其他人有帮助!

Heads up:

Sequel::MySQL.convert_invalid_date_time = nil

no longer works, but I just fixed this same problem by setting the option on my DB object:

DB = Sequel.connect("mysql://localhost/#{DB_NAME}")
DB.convert_invalid_date_time = nil

Hope this helps someone else!

哑剧 2024-08-16 16:55:24

我也有同样的问题。这是由 MySQL 的零日期“0000-00-00”上的 Sequel 阻塞引起的。我使用的解决方案是设置

Sequel::MySQL.convert_invalid_date_time = nil

(在这里找到: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

Sequel::MySQL.convert_invalid_date_time = nil

(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.

七七 2024-08-16 16:55:24

最终的解决方案涉及改用 data_objects Ruby gem。这避免了使用本机 C MySQL 驱动程序时出现的问题。

代码调整如下:

require 'rubygems'
require 'sequel'

# connect to the db
DB = Sequel.connect('do:mysql://user:pass@localhost/database')

这可能会导致性能问题,但这超出了我最初问题的范围。

感谢 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:

require 'rubygems'
require 'sequel'

# connect to the db
DB = Sequel.connect('do:mysql://user:pass@localhost/database')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文