MySqlTalk.com  

Go Back   MySqlTalk.com > MySQL > Documentation
User Name
Password
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
Thread Tools Search this Thread
Old 06-25-2004, 01:29 PM   #1
Administrator
Administrator
 
Join Date: Jun 2004
Posts: 383 Administrator is on a distinguished road
3.5 Using mysql in Batch Mode

In the previous sections, you used mysql interactively to enter queries and view the results. You can also run mysql in batch mode. To do this, put the commands you want to run in a file, then tell mysql to read its input from the file:

shell> mysql < batch-fileIf you are running mysql under Windows and have some special characters in the file that cause problems, you can do this:

dos> mysql -e "source batch-file"If you need to specify connection parameters on the command line, the command might look like this:

shell> mysql -h host -u user -p < batch-fileEnter password: ********When you use mysql this way, you are creating a script file, then executing the script.

If you want the script to continue even if some of the statements in it produce errors, you should use the --force command-line option.

Why use a script? Here are a few reasons:
  • If you run a query repeatedly (say, every day or every week), making it a script allows you to avoid retyping it each time you execute it.
  • You can generate new queries from existing ones that are similar by copying and editing script files.
  • Batch mode can also be useful while you're developing a query, particularly for multiple-line commands or multiple-statement sequences of commands. If you make a mistake, you don't have to retype everything. Just edit your script to correct the error, then tell mysql to execute it again.
  • If you have a query that produces a lot of output, you can run the output through a pager rather than watching it scroll off the top of your screen: shell> mysql < batch-file | more
  • You can catch the output in a file for further processing: shell> mysql < batch-file > mysql.out
  • You can distribute your script to other people so they can run the commands, too.
  • Some situations do not allow for interactive use, for example, when you run a query from a cron job. In this case, you must use batch mode.
The default output format is different (more concise) when you run mysql in batch mode than when you use it interactively. For example, the output of SELECT DISTINCT species FROM pet looks like this when mysql is run interactively:

+---------+| species |+---------+| bird || cat || dog || hamster || snake |+---------+In batch mode, the output looks like this instead:

speciesbirdcatdoghamstersnakeIf you want to get the interactive output format in batch mode, use mysql -t. To echo to the output the commands that are executed, use mysql -vvv.

You can also use scripts from the mysql prompt by using the source or \. command:

mysql> source filename;mysql> \. filename
Administrator is offline   Fork this post Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Import Access, Excel and data of other formats to MySQL by Navicat NavicatGuy Articles 0 08-17-2004 01:08 AM
3.2 Entering Queries Administrator Documentation 0 06-09-2004 02:58 AM
1.4.3.2 Using the MySQL Software for Free Under GPL Administrator Documentation 0 06-08-2004 02:20 AM
1.2.2 The Main Features of MySQL Administrator Documentation 0 06-08-2004 01:55 AM
1.2 Overview of the MySQL Database Management System Administrator Documentation 0 06-07-2004 10:11 PM



All times are GMT -4. The time now is 08:44 PM.



Powered by: vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
Google
  Web http://www.mysqltalk.com
DISCLAIMERS:
1. We have no commercial interest in this site.
Banner Ads and Subscriptions will only be used to help pay for hosting and maintenance costs.
2. MySQLTalk.com is NOT affiliated with MySQL AB in any way.
3. MySQLTalk.com is NOT endorsed by MySQL AB in any way.
4. Please do not post any content that is harmful to MySQL or MySQL AB, meaning no misleading or obsolete information will be tolerated.
Well-founded constructive criticism meant to help the community is permitted.
5. This website is founded with the goal of improving the MySQL community.
We not only tolerate newbies, we encourage them.
Please do not ask newbies to "read the manual".