load data infile, dealing with fields with comma

◇◆丶佛笑我妖孽 提交于 2019-12-04 09:59:55

Your content should really look like:

"139", "pg89898", "op89890", "1,000,000.00"

Then you could add the following to the command:

ENCLOSED BY '"' ESCAPED BY "\\"

And you won't have an issue.

Also, somethign you could try if you don't have any paragraphs or strings with , in them:

FIELDS TERMINATED BY ', '

You will have to alter the CSV file that is being input or alter the output that generates the CSV file - sounds the same but it isn't.

You can modify the data coming in by encapsulating fields with quotes and update your command so that it recognizes that fields are encapsulated with them using a command like ENCLOSED BY '"'

or

alter your output so that it formats the number as 1000000 rather than 1,000,000

In a CSV, comas separate "columns". Since your last value is 1,000,000.00 it is regarded as 3 different columns instead one just one (as intended).

You can either quote each value(column) or change the number format, by removing the commas (,).

if your entire file is exactly as you wrote, then maybe you could use fields terminated by ', ' (comma + space), if and only if you don't have that string within any individual value. If you are using Linux (or any other Unix like system) and your field separator is comma + space, you can use sed to replace this separator with something else:

sed 's/, /|/g' myfile.csv > myfile.txt

However, I would recommend what has already been said: modify your input file enclosing each value with quotes or double quotes and use fields terminated by ',' optionally enclosed by '"'.

Remember that your field termination character must be unique, and must not be contained within any individual value.

As a workaround, try this one -

LOAD DATA INFILE
...
FIELDS TERMINATED BY ', '
...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!