2015-09-05 45 views
0

工作如果我讓使用this documentation指定的unique_checks=0選項建立一個插入查詢,然後運行它:ActiveRecord::Base.connection.execute(query)如何獲得UNIQUE_CHECKS = 0紅寶石活動記錄

我得到一個錯誤(見下文)。但是,如果我複製包含在相同錯誤中的查詢並在沒有任何更改的情況下運行它,它就可以正常工作。所以查詢起作用,只是關於活動記錄如何運行而已。

我必須缺少一些選項才能使此工作查詢通過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; 

回答

1

可惜ActiveRecord::Base不允許在單一execute多個查詢。這是一項安全功能,您無法繞過此功能。