Saturday, 29 July 2017

How to output MySQL query results in csv format in Windows environment?



Is there an easy way to run a MySQL query from the PowerShell command line and output the results into a csv formatted file?




This question is the same as How to output MySQL query results in CSV format? except in Windows. I had to figure it out in PowerShell but my answer didn't belong on the linux question. So here's the Windows+PowerShell sibling.


Answer



Stan's answer from How to output MySQL query results in CSV format?, adapted for Windows PowerShell



mysql my_database_name -u root | Out-File .\my_output_file.csv


This gives me a mysql prompt, but without the usual mysql > at the start. I type:




source C:\Aboslute\Path\With Spac es\Without\Quotes\To\my_select_statement.sql


It gives an error message and exits if there is a problem with the command, or gives me the empty prompt if the command executed successfully. I type exit to finish up.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...