gpt4 book ai didi

mysql - 在 csv 文件中引用以导入 MySQL

转载 作者:行者123 更新时间:2023-11-29 06:07:58 24 4
gpt4 key购买 nike

在 Mac OSX 上使用 MySQL 并使用 MySQL Workbench 执行 SQL,这里是 SQL 查询和 csv 文件的内容,我想输入带引号的字符串,例如我想输入 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;

enter image description here

编辑 1,

试过用双引号编码引号,试过这种方式,导入后还是和我发的截屏一样。

1,"hello/world",,,"f""o""o"
2,"hello/world",,,"g""o""o"
3,"hello/world",,,"z""o""o"

编辑 2,试过了但没有用,(导入后,它是 foo 而不是 foo)

1,"hello/world",,,f'o'o
2,"hello/world",,,g'o'o
3,"hello/world",,,z'o'o

编辑 3,试过了还是不行,(导入后,它是 foo 而不是 foo)

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

问候,林

最佳答案

封闭字符的加倍确实应该有效。也许有一个不同的问题导致您的导入失败?还要检查原始文本输出(Query -> Execute .. to Text)或命令行客户端。

或者,您可以使用不同的封闭字符。改用单引号怎么样?如果您的文本中没有逗号(即用作字段终止符的字符),您可以完全省略封闭字符(因为在这种情况下字段终止符足以分隔条目)。

关于mysql - 在 csv 文件中引用以导入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40187074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com