Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I run mysql in a shell script

Status
Not open for further replies.

roggy

Programmer
Nov 26, 2001
5
US
I want to do a select into outfile on a scheduled basis. I can enter the commands select into outfile "/usr/local/whatever_file.txt" from my_database_table where whatever conditions; and the command works fine in the mysql shell. How can I execute it in crontab on linux redhat? It keeps giving me the reply unexpected token at 'into'
Do I need to do some kind of piping or redirection Myshell script looks kike this:

#!/bin/sh

cd /usr/local/mysql/bin;

./mysql -u anybody -h whoever --password=woopie;

select * into outfile '/usr/local/whatever_file.txt' fields terminated by ',' lines terminated by "\n" from databasename.tablename where NewReading != 0;

And I even did a chmod on the file to make it 777.
 
Never mind! I found the answer. By writing a separate file with my sql statment and then using a redirection command. It works great so far no problems.

Here's the main script:
/////////////////////////////////////////////////////
#!/bin/sh

cd /usr/local/mysql/bin;

./mysql -u anybody -h whoever
--password=woopie< /usr/local/MySQLQuery.bat;

//////////////////////////////////////////////
My query file named MySQLQuery.bat looks like this:
/////////////////////////////////////////////////////

select * into outfile '/usr/local/whatever_file.txt' fields terminated by ',' lines terminated by &quot;\n&quot; from My_database.MyTable_Name where NewReading != 0;

///////////////////////////////////////////////////////////
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top