Export Query Results from MySQL to a File

Today I figured out saving the query output of MySQL to a CSV file. I have shared the details below so that it can be useful for someone who is looking for a similar information..

I will be referring to myaddressbook table to illustrate how to export the query results to a csv file. For more information on myaddressbook table please refer my earlier post on Creating My First Table.
Lets get started, Establish MySQL Connection from Command Prompt and use testers_db as the database. Then issue the below command.

select * from myaddressbook into outfile 'c:/AddressBook.csv' fields terminated by ',' enclosed by '  ' lines terminated by '\n';

CSV File output:







Feel free to play around with the above query, try changing the extension from csv to txt, doc etc.. I am sure you should be able to see the output in these formats as well except PDF.. :) also if you want to enclose output values with apostrophe then your query will look like this.

select * from myaddressbook into outfile 'c:/AddressBook.csv' fields terminated by ',' enclosed by ' ' lines terminated by '\n';

The other way of writing is by redirecting the query output to a file. I shall write in detail about the same in my  next post.

I hope this piece of information was useful…!! Keep visiting Software Testers Forum for more working solutions and tutorials. Happy reading..!!

Export Query Results from MySQL to a File Export Query Results from MySQL to a File Reviewed by Suntaragali The Smart Techie on February 19, 2013 Rating: 5

No comments: