Query:
LOAD DATA LOCAL INFILE 'actors.csv'
INTO TABLE Actors
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ACTOR_ID, FNAME, LNAME);
CSV File:
ACTOR_ID, FNAME, LNAME
"66666","Billy","Lou"
"77777","Sally","Lou"
"88888","Hilly","Lou"
mysql> describe Actors;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| ACTOR_ID | char(5) | NO | PRI | | |
| FNAME | varchar(20) | NO | | NULL | |
| LNAME | varchar(20) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
> The output after running query:
| 10047 | Shirley | Jones |
| 10048 | Andre | Vippolis |
| 66666 | Billy | Lou"
"77777 |
| 88888 | Hilly | "Lou"
|
+----------+-------------+---------------+
我正在尝试将CSV文件放入我的数据库中。我收到了查询 来自MySQL教程(除了把我在那里的值放在那里)。当我 运行查询,我的数据未正确插入。我已经有2行了 插入(10047,10048),然后我尝试从CSV中提取数据 文件输入,但它没有正确进入。看来引用了 没有正确阅读。但声明
ENCLOSED BY '"'
应该处理报价。我在这里做错了什么?
答案 0 :(得分:1)
似乎
之间有Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;Connect Timeout=30;
\r
而不是"Lou"
"77777"
使用文本编辑器更正此问题。
找到相关的post
答案 1 :(得分:0)
CSV文件经常有回车符/换行符作为行终止符。例如,如果文件是使用Excel生成的,那么您几乎肯定会拥有该文件。
纠正这种情况的方法是修改代码,如下所示:
LOAD DATA LOCAL INFILE 'actors.csv'
INTO TABLE Actors
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(ACTOR_ID, FNAME, LNAME);
我以这种方式进行大部分CSV导入。