Mysql source command doesn't work?

I’m trying to use the mysql ‘source’ command within a ruby script to run
a batch file, but it doens’t seem to work. When I run the source
command straight from mysql, though, it runs just fine. Here’s a short
example of what I’m trying to do:

require ‘mysql’
db = Mysql.real_connect(db_host, db_user, db_pass, db_name)
filename = “/Users/test/batch.sql”
db.query “source #{filename}”

I keep getting this error when I try to do dbh.query “source
${filename}”

Error code: 1064
Error message: 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 ‘SOURCE /Users/test/batch.sql’ at line 1
Error SQLSTATE: 42000

Anyone have any ideas? Thanks in advance.

Park H. wrote:

You could use Ruby’s system like this:

system("/path/to/mysql -uuser -ppassword database <#{filename}");

That presumes the ruby script and the SQL script are colocated (and this
being MySQL, if my assumptions on users of that product hold, the
database server will also be on the same machine).

In that case, you can also read the file inside the Ruby process and
then feed the commands to the database connection. This has the
advantage that you don’t show your DB username and password to all and
sundry connected to that machine in ps output. (Passwords on command
lines are bad, 'mmmkay.)

David V.

Hi,

Error message: 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 ‘SOURCE /Users/test/batch.sql’ at line 1
Error SQLSTATE: 42000

Anyone have any ideas? Thanks in advance.

‘source’ is not an SQL command, but an internal command of the mysql
command
line client.

You could use Ruby’s system like this:

system("/path/to/mysql -uuser -ppassword database <#{filename}");

Regards,

Park H.