在Mac OSX上使用MySQL,在MySQL Workbench上执行SQL,这里是csv文件的SQL查询和内容,我想输入带引号的字符串,例如,我想输入f"o"o
,我想使用f\"o\"o
,但似乎不起作用,有什么想法可以用引号导入csv文件中的一列吗?
SQL查询,
LOAD DATA LOCAL INFILE '/Users/foo/Downloads/import.csv'
INTO TABLE tasks
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
表架构,
create database exercise;
CREATE TABLE IF NOT EXISTS tasks (
task_id INT(11) NOT NULL AUTO_INCREMENT,
subject VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
CSV文件内容,
1,"hello/world",,,"f\"o\"o"
2,"hello/world",,,"g\"o\"o"
3,"hello/world",,,"z\"o\"o"
导入后的内容,
SELECT * FROM exercise.tasks;
编辑1,
尝试使用双引号对引号进行编码,尝试了这种方式,导入后仍与我发布的屏幕快照相同。
1,"hello/world",,,"f""o""o"
2,"hello/world",,,"g""o""o"
3,"hello/world",,,"z""o""o"
编辑2,尝试过但不起作用(导入后,它是
foo
而不是fo
o)1,"hello/world",,,f'o'o
2,"hello/world",,,g'o'o
3,"hello/world",,,z'o'o
编辑3,尝试过,但也不起作用(导入后,它是
foo
而不是fo
o)1,"hello/world",,,"f''o''o"
2,"hello/world",,,"g'o''o"
3,"hello/world",,,"z''o''o"
编辑4,警告我看到,
LOAD DATA LOCAL INFILE '/Users/foo/Downloads/import.csv' INTO TABLE tasks FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' 3 row(s) affected, 6 warning(s): 1265 Data truncated for column 'start_date' at row 1 1265 Data truncated for column 'end_date' at row 1 1265 Data truncated for column 'start_date' at row 2 1265 Data truncated for column 'end_date' at row 2 1265 Data truncated for column 'start_date' at row 3 1265 Data truncated for column 'end_date' at row 3 Records: 3 Deleted: 0 Skipped: 0 Warnings: 6
当做,
林
最佳答案
将封闭的char加倍确实应该有效。可能是另一个问题导致导入失败?还可以使用原始文本输出(Query -> Execute .. to Text
)或命令行客户端进行检查。
或者,可以使用不同的封闭字符。用一个引号代替怎么样?如果文本中没有逗号(即用作字段结束符的字符),则可以完全省略封闭字符(因为在这种情况下,字段结束符足以分隔条目)。
关于mysql - CSV文件中的引号导入MySQL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40187074/