[英]How to export a CSV file from the MySQL command line
I have a MySQL database containing a single table which is used as a temporary storage point for manipulating and querying data sets and is periodically deleted and replaced with new data.我有一个包含单个表的 MySQL 数据库,该数据库用作操作和查询数据集的临时存储点,并定期删除并替换为新数据。 What I would like to be able to do is export the data from the MySQL command line to use for other purposes.
我希望能够做的是从 MySQL 命令行导出数据以用于其他目的。 I am using the XAMPP Apache server package with phpMyAdmin on Windows 10.
我在 Windows 10 上使用带有 phpMyAdmin 的 XAMPP Apache 服务器包。
The issue I am having is the INTO OUTFILE syntax I am using returns an error relating to '\\n'.我遇到的问题是我使用的 INTO OUTFILE 语法返回与“\\n”相关的错误。 Below is an example of the syntax:
下面是一个语法示例:
SELECT *
FROM tablename
WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "''"
ESCAPED BY '\'
LINES TERMINATED BY '\n';
I have spent some time researching this without any luck, I have even tried using我花了一些时间研究这个没有任何运气,我什至尝试使用
LINES TERMINATED BY '\r\n'
but the error remained the same但错误保持不变
ERROR: Unknown command '\''.
-> LINES TERMINATED BY '\r\n';
If anyone could provide any tips that would be greatly appreciated.如果有人可以提供任何提示,将不胜感激。
The problem is not in LINES TERMINATED BY
but in ESCAPED BY
.问题不在
LINES TERMINATED BY
而在ESCAPED BY
。
This:这个:
ESCAPED BY '\'
Is invalid syntax, because the backslash is interpreted as an escape character for the following quote.语法无效,因为反斜杠被解释为以下引号的转义字符。 A decent text editor should let you see that.
一个体面的文本编辑器应该让你看到这一点。
You need to escape the backslash, like so:您需要转义反斜杠,如下所示:
ESCAPED BY '\\'
Alternatively, you can also use '\\b'
:或者,您也可以使用
'\\b'
:
ESCAPED BY '\b'
Another probem is that OPTIONALLY ENCLOSED
accepts only a single character, while you are giving it two single quotes.另一个问题是
OPTIONALLY ENCLOSED
只接受一个字符,而你给它两个单引号。
In your query:在您的查询中:
SELECT * FROM tablename WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY "'" ESCAPED BY '\\'
LINES TERMINATED BY '\n'
Use this用这个
SELECT *
FROM tablename
#WHERE work_complete = 'Yes'
INTO OUTFILE 'C:\file path for the file to be exported to\file_name.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
I used some other syntax as you and deleted the OPTIONALLY which mysql doesn't like at that place我和你一样使用了其他一些语法,并删除了 mysql 在那个地方不喜欢的 OPTIONALLY
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.