Professional Web Applications Themes

running sql scripts from perl script - MySQL

I am not sure if this probleem is primarily perl or MySQL, so I'll try here first. With any luck, there is someone else here who uses perl to run sql scripts against a MySQL DB. First, the following is a command that I can run from the Windows console, and it works as expected. mysql -u my_uid --password=my_pword --database=my_db < C:\DesignDocs\load_data.sql Here are two versions of the relevant statement in my perl script that do not work: my args = ("mysql","--user=my_uid","--password=my_pword","--database=my_db","<","C:\DesignDocs\load_data.sql "); print system(args); my args = ("mysql","--user=my_uid","--password=mypword","--database=my_db","<","C:\DesignDocs\load_data.sql "); print system(args); Both of these give me, as output, the ...

  1. #1

    Default running sql scripts from perl script

    I am not sure if this probleem is primarily perl or MySQL, so I'll try
    here first. With any luck, there is someone else here who uses perl to
    run sql scripts against a MySQL DB.

    First, the following is a command that I can run from the Windows
    console, and it works as expected.

    mysql -u my_uid --password=my_pword --database=my_db <
    C:\DesignDocs\load_data.sql

    Here are two versions of the relevant statement in my perl script that
    do not work:

    my args =
    ("mysql","--user=my_uid","--password=my_pword","--database=my_db","<","C:\\DesignDocs\\load_data.sql ");
    print system(args);


    my args =
    ("mysql","--user=my_uid","--password=mypword","--database=my_db","<","C:\\DesignDocs\\load_data.sql ");
    print system(args);

    Both of these give me, as output, the page describing proper usage of
    mysql, suggesting that mysql is not getting the parameters as I
    specified them.

    I expect I can work around this by creating a simple batch file, but
    that doesn't explain why my perl statements don't work when, as far as
    I can tell from the doentation I have at hand, they should have
    worked.

    Can anyone out there solve this puzzle?

    Thanks

    Ted

    Ted Guest

  2. #2

    Default Re: running sql scripts from perl script

    Peter H. Coffin wrote:
    > On 22 May 2006 13:47:19 -0700, Ted wrote:
    >> I am not sure if this probleem is primarily perl or MySQL, so I'll
    >> try here first. With any luck, there is someone else here who uses
    >> perl to run sql scripts against a MySQL DB.
    >>
    >> First, the following is a command that I can run from the Windows
    >> console, and it works as expected.
    >>
    >> mysql -u my_uid --password=my_pword --database=my_db <
    >> C:\DesignDocs\load_data.sql
    >>
    >> Here are two versions of the relevant statement in my perl script
    >> that do not work:
    >>
    >> my args =
    >> ("mysql","--user=my_uid","--password=my_pword","--database=my_db","<","C:\\DesignDocs\\load_data.sql ");
    >> print system(args);
    >>
    >>
    >> my args =
    >> ("mysql","--user=my_uid","--password=mypword","--database=my_db","<","C:\\DesignDocs\\load_data.sql ");
    >> print system(args);
    >>
    >> Both of these give me, as output, the page describing proper usage of
    >> mysql, suggesting that mysql is not getting the parameters as I
    >> specified them.
    >>
    >> I expect I can work around this by creating a simple batch file, but
    >> that doesn't explain why my perl statements don't work when, as far
    >> as I can tell from the doentation I have at hand, they should have
    >> worked.
    >>
    >> Can anyone out there solve this puzzle?
    >
    > "<" is not an argument. It is a shell-dependant redirect of input
    > from a file instead of a terminal keyboard.
    Yes, and he is trying to get the contents of the load_data.sql file to be
    redirected as input.


    Paul Lautman Guest

  3. #3

    Default Re: running sql scripts from perl script

    Ted wrote:
    > mysql -u my_uid --password=my_pword --database=my_db <
    > C:\DesignDocs\load_data.sql
    This illustrates something you should understand about shell command
    processing if you're going to use the system() call from Perl.

    The shell don't pass all the args you see above to the child process.
    Specifically, redirects with ">" or "<" or "|" are handled by the shell,
    by remapping the file descriptors of the child process.

    Once the file descriptors are set up for the child process, then the
    remainder of the command line is passed to that child process as it
    exec's the named program. This is basically the way most UNIX shells
    work, with respect to I/O redirection.

    If you want to learn more about this, read POSIX C API docs on system
    calls like dup(), dup2(), execl(), fork(), etc.

    As far as I can tell, the modern "cmd.exe" on Windows works the same,
    though I believe the older DOS command shells actually pass the "<"
    redirection symbol to the child processes as arguments. But the mysql
    client assumes it's being run by the modern type of shell, and does not
    recognize "<" as an argument.

    The mysql client application provides a way to load a script without
    redirecting I/O. You can execute the literal client statement "SOURCE
    <file>" from the command line, using the mysql -e option.

    For example:

    my args =
    ("mysql","--user=my_uid","--password=my_pword","--database=my_db","-e","source
    C:\\DesignDocs\\load_data.sql");
    print system(args);

    Regards,
    Bill K.
    Bill Karwin Guest

  4. #4

    Default Re: running sql scripts from perl script

    Bill Karwin wrote:
    > Ted wrote:
    >
    >> mysql -u my_uid --password=my_pword --database=my_db <
    >> C:\DesignDocs\load_data.sql
    >
    >
    > This illustrates something you should understand about shell command
    > processing if you're going to use the system() call from Perl.
    >
    > The shell don't pass all the args you see above to the child process.
    > Specifically, redirects with ">" or "<" or "|" are handled by the shell,
    > by remapping the file descriptors of the child process.
    >
    > Once the file descriptors are set up for the child process, then the
    > remainder of the command line is passed to that child process as it
    > exec's the named program. This is basically the way most UNIX shells
    > work, with respect to I/O redirection.
    >
    > If you want to learn more about this, read POSIX C API docs on system
    > calls like dup(), dup2(), execl(), fork(), etc.
    >
    > As far as I can tell, the modern "cmd.exe" on Windows works the same,
    > though I believe the older DOS command shells actually pass the "<"
    > redirection symbol to the child processes as arguments. But the mysql
    > client assumes it's being run by the modern type of shell, and does not
    > recognize "<" as an argument.
    >
    > The mysql client application provides a way to load a script without
    > redirecting I/O. You can execute the literal client statement "SOURCE
    > <file>" from the command line, using the mysql -e option.
    >
    > For example:
    >
    > my args =
    > ("mysql","--user=my_uid","--password=my_pword","--database=my_db","-e","source
    > C:\\DesignDocs\\load_data.sql");
    > print system(args);
    >
    > Regards,
    > Bill K.
    Bill,

    No, the old DOS shell interpreted redirection symbols itself ever since they
    were first implemented (DOS 2.0? 2.1? somewhere back then).


    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    [email]jstucklexattglobal.net[/email]
    ==================
    Jerry Stuckle Guest

  5. #5

    Default Re: running sql scripts from perl script

    Jerry Stuckle wrote:
    > No, the old DOS shell interpreted redirection symbols itself ever since
    > they were first implemented (DOS 2.0? 2.1? somewhere back then).
    Interesting. I believe you that the I/O was handled by the DOS shell,
    but I could swear I've seen return messages from applications that made
    me think that the "<" arg was also passed to the app. I must have made
    an incorrect assumption.

    In any case, DOS is dead so it doesn't matter. ;-)

    Regards,
    Bill K.
    Bill Karwin Guest

  6. #6

    Default Re: running sql scripts from perl script

    Thanks Bill, Peter, Paul and Jerry.

    Your input is appreciated.

    Bill, your example proved valuable, and worked like a charm.

    Thanks again.

    Ted

    Ted Guest

Similar Threads

  1. Running compiled Inline C perl scripts on more than one machine
    By ant_howell@yahoo.com in forum PERL Modules
    Replies: 2
    Last Post: February 10th, 11:23 AM
  2. Re-running Perl's configure script.
    By Daniel Staal in forum PERL Beginners
    Replies: 0
    Last Post: September 23rd, 07:11 PM
  3. ENV variables in perl scripts called from perl.
    By Sandman in forum PHP Development
    Replies: 2
    Last Post: July 29th, 10:58 AM
  4. Running a Perl Script in ASP.NET
    By Titof in forum ASP.NET General
    Replies: 0
    Last Post: July 23rd, 01:29 PM
  5. Perl script not running properly...
    By abstractjoe in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 11th, 06:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139