使用 dbi gem 的 ruby 脚本在使用新索引插入 mysql 表时出错
我有一个 ruby 脚本,使用 dbi gem 重复执行以下 INSERT 语句:
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}');"
该脚本一直运行良好,直到几天前出现以下错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 ' '')' 附近使用的正确语法。
该错误似乎是从在其插入的表上添加 object_id 和 object_type 索引开始的。我得出这个结论是因为如果我不为 object_id 插入值,该语句就会神奇地起作用。另外,我从我们的开发版本的表中删除了索引,然后就成功了!它又起作用了。我需要保留这个索引。
有人处理过这个问题吗?我即将使用活动记录重写脚本。整个脚本从 MS SQL Server 数据库中提取数据并将其插入到 MySQL 数据库中。
我还应该补充一点,当涉及到 Ruby 编程时,我真的不知道自己在做什么,而且我不确定这个脚本最终是如何用 Ruby 编写的。任何帮助将不胜感激。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来可能是一个引用问题。仔细查看错误消息:
您在 SQL 中得到一个空格,后跟一个空字符串文字,后跟一个右括号。您添加了
object_id
和object_type
列,但一切都损坏了,因此问题可能出在object_type
值中。您不应该使用字符串插值来构建 SQL 语句,而应该使用占位符。其中一种方法可能会有所帮助:
它开始和停止工作很奇怪,但这可能只是调试工作的副作用。
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_id
andobject_type
columns and everything broke so the problem is probably in theobject_type
values.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.