简体   繁体   中英

MySQL Exporting query output to CSV

[MySQL Workbench 6.2 on Windows7]

I can export the query results clicking in the MySQL workbench export icon. See image below:

手动将查询结果导出到CSV文件的方法

However, I need to do this repeatedly in different loops, so I would like to include it in my script.

I have tried:

SELECT * from TABLENAME where ID = 123456 INTO OUTFILE 'C:/Users/username/Desktop/test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n';

I also tried the filepath with \\\\ instead of / , and also with LINES TERMINATED BY '\\r \\n' as I have seen in other posts.

When I tried this, I get Permission Denied [Errcode 13] despite I have grated my user file permissions in the MySQL command client too with the following code:

USE mysql;
UPDATE user SET File_priv = 'Y' WHERE User = 'db_user';
FLUSH PRIVILEGES;

Any ideas why it is still not working? Any good alternative is also welcome!

This is due to folder access permission for mysql. So use below path to write csv file.

C:\\Users\\<user_name>\\AppData\\Local\\Temp

Let suppose your system has user name ABC then your query should be.

SELECT * from TABLENAME where ID = 123456 INTO OUTFILE 'C:\\Users\\ABC\\AppData\\Local\\Temp\\test.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM