I want to do the following mysql -uuser -ppass -h remote.host.tld database < script.sql
where script.sql
contains the following
SELECT *
FROM webrecord_wr25mfz_20101011_175524
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
I want CSV output directed to standard out. The reason is because running this query with an INTO OUTFILE 'blah.csv'
will save the file on the remote host. I want the file saved on the local host.
If I could just redirect the standard output to a file, that would be dandy.
Select the table of the database that you want to export and click on the Export tab from the right side. Select the CSV format from the Format drop-down list and click on the Go button. Select the Save File option and press the OK button. The file will be downloaded in the Downloads folder.
The answers above don't seem to fully answer the original question, and I'm not sure if this does either, but hopefully this might help someone:
See How to output MySQL query results in CSV format? for a lot of comments regarding how to sed
. For example, based on the original parameters, the following might be sufficient:
mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | sed 's/\t/,/g' 2>&1
This is similar to the answer above, but redirecting to stdout
instead of blah.csv
.
However, (although not sure if this will work if you need to preserve tabs, there are many ways to address this though), I've used https://stackoverflow.com/a/2543226/2178980 to correctly escape double quotations and convert to comma-separated:
mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | perl -lpe 's/"/\\"/g; s/^|$/"/g; s/\t/","/g' 2>&1
sql
"SELECT * FROM webrecord_wr25mfz_20101011_175524;"
via mysql
(this output will be tab-separated)perl -lpe 's/"/\\"/g; s/^|$/"/g; s/\t/","/g'
stdout
by appending 2>&1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With