简体   繁体   中英

mysqldump selected table in rails

I want to dump some mysql tables by mysqldump command in rails, but doesn't work.

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)

Error messages:

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)

Hope someone can help me , thanks in advance!

You are trying to execute a shell command as SQL. These are not the same things. You can run it from the command line and achieve the effect you are looking for.

If this needs done in code it can be done like this:

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

multiple problems here:

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

you need to specify the mysql password after the -p{put--password-here} and get rid of the semi-colon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM