mysql source command doesn't work?

  • Thread starter Raymond O'connor
  • Start date
R

Raymond O'connor

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.
 
P

Park Heesob

Hi,
From: Raymond O'connor <[email protected]>
Reply-To: (e-mail address removed)
To: (e-mail address removed) (ruby-talk ML)
Subject: mysql source command doesn't work?
Date: Sat, 2 Dec 2006 10:21:49 +0900

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.

'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 Heesob

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
 
D

David Vallner

--------------enigC2D64F734A2CC3FD45B7EE52
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Park said:
=20
'source' is not an SQL command, but an internal command of the mysql
command line client.
=20
You could use Ruby's system like this:
=20
system("/path/to/mysql -uuser -ppassword database <#{filename}");
=20

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 Vallner


--------------enigC2D64F734A2CC3FD45B7EE52
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFcWMIy6MhrS8astoRApbKAJ4ojQtBDTeCBWULi8RLOWscGhyKIQCggGu1
/I34RqZwxSRLs36FlIHLhIg=
=GrKF
-----END PGP SIGNATURE-----

--------------enigC2D64F734A2CC3FD45B7EE52--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top