bash 将数据加载到 MySQL:如何处理反斜杠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15684286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading data into MySQL: How to deal with backslashes?
提问by ktm5124
I downloaded a tab-delimited file from a well-known source and now want to upload it into a MySQL table. I am doing this using load data local infile.
我从知名来源下载了一个制表符分隔的文件,现在想将其上传到 MySQL 表中。我正在使用load data local infile.
This data file, which has over 10 million records, also has the misfortune of many backslashes.
这个拥有超过1000万条记录的数据文件,也有很多反斜杠的不幸。
$ grep '\' tabd_file.txt | wc -l
223212
These backslashes aren't a problem, except when they come at the end of fields. MySQL interprets backslashes as an escape character, and when it comes at the end of the field, it messes up the next field, or possibly the next row.
这些反斜杠不是问题,除非它们出现在字段末尾。MySQL 将反斜杠解释为转义字符,当它出现在字段的末尾时,它会混淆下一个字段,或者可能是下一行。
In spite of these backslashes, I only received 6 warnings from MySQL when loading it into a table. In each of these warnings, a row doesn't have the proper number of columns precisely because the backslash concatenated two adjacent fields in the same row.
尽管有这些反斜杠,但在将其加载到表中时,我只收到了来自 MySQL 的 6 条警告。在这些警告中的每一个中,由于反斜杠将同一行中的两个相邻字段连接在一起,因此行没有正确的列数。
My question is, how to deal with these backslashes? Should I specify load data local infile [...] escaped by ''to remove any special meaning from them? Or would this have unintended consequences? I can't think of a single important use of an escape sequence in this data file. The actual tabs that terminate fields are "physical tabs", not "\t" sequences.
我的问题是,如何处理这些反斜杠?我应该指定load data local infile [...] escaped by ''删除它们的任何特殊含义吗?或者这会产生意想不到的后果?我想不出这个数据文件中转义序列的一个重要用途。终止字段的实际选项卡是“物理选项卡”,而不是“\t”序列。
Or, is removing the escape character from my load command bad practice? Should I just replace every instance of '\'in the file with '\\'?
或者,从我的加载命令中删除转义字符是不好的做法吗?我应该用 替换'\'文件中的每个实例'\\'吗?
Thanks for any advice :-)
感谢您的任何建议:-)
回答by koriander
If you don't need the escaping, then definitely use ESCAPED BY ''.
如果您不需要转义,那么一定要使用 ESCAPED BY ''。
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
"If the FIELDS ESCAPED BY character is empty, escape-sequence interpretation does not occur. "
“如果 FIELDS ESCAPED BY 字符为空,则不会发生转义序列解释。”

