Command line tricks
By admin on May 30, 2008 | In Daily Commute - Standard hints/tips | 2 feedbacks »
One of the most frustrating things I found when starting to use Mysql, was the difficulty in interracting with the command line (I use Linux) when inside the Mysql environment. Here are some useful commands I've found (I'll edit this if I find any more)
Code:
\! cat case1.sql |
-- enters shell mode and (in this case with cat) displays the code for case1.sql
This makes it possible to save your sql statements from the sql command line into a file on your local host. (Of course, with large statements it's probably better to have 2 sessions open and cut and paste across)
Code:
\! echo 'select * from emp where job ="salesman" ' > test2.sql |
There will be those times when you need to run a sql statement you've previously saved:
Code:
\. case1.sql OR source case1.sql |
-- executes the file case1.sql within mysql.
Of course you may wish to run a sql statement from the shell command line.
If you enter
Code:
mysql -u root -p < case1.sql |
then you are prompted for the password. Also, you have to make sure that the case1.sql statement contains the statement use dbname;
You can do this instead:
Code:
mysql -u root -ptiger one < case1.sql |
Here the password can be input on the command line so long as you don't leave a gap after the -p switch (don't hard-code this on command files though!) and you can specify the database name (in this case one) that you are using.
If you know of any other useful hints on this subject please feel free to leave feedback - it will be gratefully received!
I hope also that people are finding these entries of use.
EDIT: Just found out about the tee facility. If you enter \T filename
you will then have your statements and query results directed/printed to the filemame you specified. Use \t to turn this off.
Another tip that may help - when putting together your sql statement in the Mysql command line, you can edit it using your default editor by ending the query with "\e" instead of ";" or "\G".
2 comments
This post has 3 feedbacks awaiting moderation...
Leave a comment
| « Experimenting with ORDER BY | A daily quote with php/Mysql » |