【问题标题】:strftime error while switching my database from sqlite3 to mysql in Rails4在 Rails4 中将我的数据库从 sqlite3 切换到 mysql 时出现 strftime 错误
【发布时间】:2017-03-28 05:53:16
【问题描述】:

最近我将我的数据库从 sqlite3 更改为 mysql。当我运行我的项目时,我在使用此查询的文件之一中收到此错误。

日期 = 日期.今天 + 1

@employees = Employee.where("status = ? AND strftime('%d/%m', date_of_birth) = ?", "Active" , date.strftime('%d/%m')

ActionView::Template::Error (Mysql2::Error: FUNCTION hrms_development.strftime does not exist: SELECT COUNT(*) FROM `employees` WHERE (status = 'Active' AND strftime('%d/%m', date_of_birth) = '28/03')):
    210:             
    211:               <% date = Date.today %>
    212:              <% @employees = Employee.where("status = ? AND strftime('%d/%m', date_of_birth) = ?", "Active" , date.strftime('%d/%m')) %>
    213:             <% if @employees.empty? %>
    214:              
    215:             <%else%>
    216:               <% @employees.each do |e| %>
  app/views/home/_group_admin.html.erb:213:in `_app_views_home__group_admin_html_erb___2522183600721478262_91627100'
  app/views/home/index.html.erb:17:in `_app_views_home_index_html_erb__2772204906267359422_86967120'



ActionView::Template::Error: Mysql2::Error: FUNCTION hrms_development.strftime does not exist: SELECT COUNT(*) FROM `employees` WHERE (status = 'Active' AND strftime('%d/%m', date_of_birth) = '28/03')

【问题讨论】:

    标签: mysql ruby-on-rails-4 sqlite


    【解决方案1】:

    用简单的替换试试这个,

    <% @employees = Employee.where("status = ? AND strftime('%d/%m', date_of_birth) = ?", "Active" , date.strftime('%d/%m')) %>
    

    <% @employees = Employee.where("status = ? AND DATE_FORMAT(date_of_birth,'%d/%m') = ?", "Active" , date.strftime('%d/%m')) %>
    

    【讨论】:

    • 我在运行我的项目并执行 rake db:migrate 时在 Windows 上收到此错误
    • D:/Ruby21-x64/lib/ruby/gems/2.1.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require': 不能t 加载“active_record/connection_adapter/mysql2_adapter”。确保 config/database.yml 中的适配器有效。如果您使用“mysql”、“mysql2”、“postgresql”或“sqlite3”以外的适配器,请将必要的适配器 gem 添加到 Gemfile。 (加载错误)
    • 在 Linux ubuntu 上一切正常,只有在 windows 上我得到这个错误。
    • 你需要在windows机器上安装mysql..以及
    • 我在 mysql 中导出我的数据库时遇到问题,我已经运行了 mysqldump -h [host] -u [uname] -p[pass] db_name > db_backup.sql 然后是 database.sql,并且也源database.sql仍然没有表中的数据??????
    【解决方案2】:

    MySQL 中没有 strftime 函数。请改用DATE_FORMAT

    更多示例请参见Convert DateTime Value into String in Mysql

    【讨论】:

    • 您能否提供一个示例,说明如何在 Rails 4 中应用 DATE_FORMAT。我是新手,所以不知道。
    • date = Date.today @employees = Employee.where("status = ? AND strftime('%d/%m', date_of_birth) = ?", "Active" , date.strftime(' %d/%m'))
    • 我想将此查询转换为mysql。
    • 我的新问题......stackoverflow.com/questions/43065691/…@hardik
    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    相关资源
    最近更新 更多