Pregunta

I have a CRON job which executes a SELECT statement to grab records. When the SELECT runs on my dev machine, it produces the following statement:

SELECT `users`.* FROM `users` WHERE `users`.`id` = 87 LIMIT 1  

This is successful.

When the SELECT runs on my production (hosted) machine it produces the statement with double quotes:

SELECT "users".* FROM "users" WHERE "users”.”id” = 87 LIMIT 1

This is not successful and I get a MySQL 1064 error,

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.* FROM "users" WHERE "users

The code is the same on both machines, but my dev MySQL is version 5.5.33, whereas production is 5.1.67 (I don't have control over this to set/update it)

Is there a way to force single quotes or another preferred method to handle this situation?

Thanks for your time and assistance.

--EDIT--

Here are the main code snippets that are invoked via my CRON job:

/lib/tasks/reports.rake

namespace :report do
  desc "Send Daily Report"
  task :daily => :environment do
    User.where(:report_daily => 1).find_each do |user|
      ReportsMailer.send_report(user, 'daily').deliver
    end
  end

/app/mailers/reports_mailer.rb

def send_report(user, date_increment)
   @user = user
   @date_increment = date_increment
   get_times(user)
   mail :to => user.email, :subject=> "Report: #{@dates}"
 end

--EDIT2--

So it looks like I need to use slanted single quotes (`) in order for this to work successfully. How do I force my app or MySQL to use these instead of double (") quotes?

¿Fue útil?

Solución 3

After further inspection, and working with my hosting company, its turns out that my query is timing out on their server. Thanks to all that responded.

Otros consejos

I don't know why it does this, but I do know that if you're referencing column names in MYSQL, you need to use ``, whereas values / data should be wrapped in "", like this:

SELECT `users`.* FROM `users` WHERE `users`.`id` = "87" LIMIT 1 

I learnt this the hard way back in the day when I was learning how to do simple MYSQL queries

Here's some documentation from MYSQL's site for you:

The identifier quote character is the backtick (“`”):

mysql> SELECT * FROM `select` WHERE `select`.id > 100;

Identifier quote characters can be included within an identifier if you quote the identifier. If the character to be included within the identifier is the same as that used to quote the identifier itself, then you need to double the character. The following statement creates a table named a`b that contains a column named c"d:

mysql> CREATE TABLE `a``b` (`c"d` INT);

Is there any reason you couldn't put some of your sql statement directly into your code like:

User.where("`report_daily`=1").find_each do |user|

Since you are not using any literals, the format of the generated SQL statements should be determined by the underlying adapter. Perhaps you have a different mysql adapter installed or configured on each machine. Check the installed version. For example:

bundle show mysql

and also check the adapter configuration for your project in database.yml. For example:

adapter: mysql

A comparison of the results of these checks between each machine should tell you if you are using different adapters on the two machines.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top