Link to home
Start Free TrialLog in
Avatar of paul_tipper
paul_tipper

asked on

Passing command line parameters to a MySQL script

I want to run a set of MySQL queries in command line/batch mode from a script file, but I also want to be able to pass parameters from the command line and refer to them in the script file, similar to how command line parameters can be referred to in bash/shell scripts as $1, $2, .. etc. So, the MySQL query file will be invoked as follows:

mysql -u userid -p password dbname <deleteoldrows.sql 10

deleteoldrows.sql will contain a query along the following lines:

$1 in the snippet should be substituted with the '10' passed from the command line. I've already tried this, but needless to say, it didn't work (@1 didn't work either).

Can anyone tell me whether passing command line arguments through to MySQL scripts is possible, and if so, how is it done?
DELETE FROM debuglog WHERE TIMESTAMPDIFF(DAY, eventtime, NOW()) > $1;

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I don't think that is possible...
Avatar of paul_tipper
paul_tipper

ASKER

So, I guess I should probably best accomplish this by embedding the MySQL queries into a PHP CLI script instead?
yes, I do that frequently with web pages, but not from command line...
ASKER CERTIFIED SOLUTION
Avatar of NovaDenizen
NovaDenizen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, of course, that'll certainly work - I'd forgotten you could use the mysql command as a filter.

Thanks a lot!
There's also this:
mysql -u userid -p password dbname -e "DELETE FROM debuglog WHERE TIMESTAMPDIFF(DAY, eventtime, NOW()) > $1;"