如果我使用this documentation中指定的unique_checks=0
选项构建插入查询,然后使用:ActiveRecord::Base.connection.execute(query)
我收到错误(见下文)。但是,如果我复制包含在同一错误中的查询并在没有任何mysql更改的情况下运行它就可以了。所以查询工作,只是关于活动记录如何运行它正在打破它。
为了让这个有效的查询能够通过ActiveRecord::Base.connection.execute
,我必须找不到一些选项。任何帮助都会非常感激!
#Error Message
=> #<ActiveRecord::StatementInvalid: Mysql2::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 'SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, st' at line 2: SET autocommit=0;
SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","8608878927");
SET unique_checks=1;
COMMIT;>
#Working SQL Query I stripped out of above error message
SET autocommit=0;
SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","608878927");
SET unique_checks=1;
COMMIT;
答案 0 :(得分:1)
不幸的是,ActiveRecord::Base
不允许在一个execute
中进行多次查询。这是一项安全功能,您无法绕过此功能。