I have a ruby script repeatedly executing the following the INSERT statement using the dbi gem:
sql = "INSERT INTO traffic_by_pages (publication_id, subdomain, region_id, region_name, page_url, site_section, yr, mnth, dy, TrafficDate, DailyVisitors, DailyVisits, DailyPageViews, NewVisitors, ReturningVisitors, object_id, object_type) VALUES (#{publication_id}, '#{subdomain}', #{region_id}, '#{region_name}', '#{page_url}', '#{site_section}', #{yr}, #{mnth}, #{dy}, '#{trafficdate}', #{dailyvisitors}, #{dailyvisits}, #{dailypageviews}, #{newvisitors}, #{returningvisitors}, #{obj_id}, '#{objectType}');"
This script was working perfectly well until a few days ago with the following error:
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 ‘ ”)’.
The error seems to have started with the addition of an index on object_id and object_type on the table it is inserting into. I have made this conclusion because if I do not insert a value for object_id, the statement magically works. Also, I took the index off our dev version of the table, and oila! It worked again. I need to keep this index.
Has anyone ever dealt with this issue? I am about to re-write the script using active record. The whole script pulls data from MS SQL Server database and INSERTS into a MySQL database.
I should also add when it comes to programming in Ruby I really don’t know what I am doing, and I am not sure how this script ended up being written in Ruby. Any help would be much appreciated. Thanks!
This sounds like it could be a quoting problem. Look closely at the error message:
You’re getting a space followed by an empty string literal followed by a closing parenthesis in your SQL. You added the
object_idandobject_typecolumns and everything broke so the problem is probably in theobject_typevalues.You shouldn’t be using string interpolation to build SQL statements, you should be using placeholders. One of these approaches might help:
That it starts and stops working is strange but that could just be a side effect of your debugging efforts.