2014-11-25 38 views
1

我想通過mysqldump命令在rails中轉儲一些mysql表,但是不起作用。mysqldump在rails中的選定表

client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => 'sparta_development') 

sql = "mysqldump -u root -p sparta_development config_products > config_products.sql;" 

client.query(sql) 

錯誤消息:

in `query': 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 'mysqldump -u root -p sparta_development config_products > config_products.sql' at line 1 (Mysql2::Error) 

#mysql --version 
mysql Ver 14.14 Distrib 5.5.25a, for osx10.6 (i386) using readline 5.1 
#gem list |grep my 
mysql2 (0.3.14) 

希望有人能幫助我,在此先感謝!

回答

3

您試圖執行一個shell命令作爲SQL。這些不是一回事。您可以從命令行運行它並獲得您正在尋找的效果。

如果需要在代碼中完成它可以這樣做:

system 'mysqldump -u root -p sparta_development config_products > config_products.sql;' 
0

多個這裏的問題:

sql = "mysqldump -u root -p sparta_development config_products > config_products.sql;" 

您需要的-p {put--後指定mysql的密碼密碼 - 在這裏}並擺脫分號。