mysql source command doesn't work?

Discussion in 'Ruby' started by Raymond O'connor, Dec 2, 2006.

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Raymond O'connor, Dec 2, 2006
    #1
    1. Advertising

  2. Raymond O'connor

    Park Heesob Guest

    Hi,

    >From: Raymond O'connor <>
    >Reply-To:
    >To: (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/
     
    Park Heesob, Dec 2, 2006
    #2
    1. Advertising

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

    Park Heesob wrote:
    >> I'm trying to use the mysql 'source' command within a ruby script to r=

    un
    >> 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 shor=

    t
    >> example of what I'm trying to do:
    >>
    >> Anyone have any ideas? Thanks in advance.
    >>

    >=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--
     
    David Vallner, Dec 2, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZG9ybGluZw==?=
    Replies:
    0
    Views:
    560
    =?Utf-8?B?ZG9ybGluZw==?=
    Jun 24, 2005
  2. JL
    Replies:
    0
    Views:
    1,165
  3. P. A.
    Replies:
    0
    Views:
    152
    P. A.
    Sep 4, 2009
  4. Jack
    Replies:
    1
    Views:
    153
    Paul Lalli
    Mar 22, 2007
  5. Kuhl
    Replies:
    1
    Views:
    286
    Tad J McClellan
    Dec 22, 2008
Loading...

Share This Page