如何将包含一些空单元格的 CSV 文件加载到 MySQL?
How can a CSV file with some empty cells be loaded into MySQL?
我正在尝试将包含一些空单元格的 CSV 文件导入 MySQL 数据库。你能帮我吗?
我的上一个线程已关闭,因为它“与 similar question 相关联”。我尝试了该解决方案但没有奏效,下面列出了对此的尝试。
我创建了一个table:
CREATE TABLE citytable (
id int(11) NOT NULL auto_increment,
city varchar(100),
number int(11) NOT NULL DEFAULT 1,
comment varchar(100),
primary key(id)
);
我已经用数据填充了一个 csv,并想将其导入 table
CSV 文件csvfile.csv
:
id,city,number,comment
1,NY,1,Something
2,W,2,
3,C,1,Something
4,LA,1,
所以我使用以下命令加载 CSV:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id,city,number,@vcomment)
SET comment = NULLIF(@vcomment,'');
我也试过将 comment
的默认值设置为 ''
并重新运行命令并得到错误 ``ERROR 1261 (01000): 第 4 行不包含所有列的数据`.
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id,city,number,comment);
如果该单元格中没有数据,我如何只导入数据库并让值为“NULL”?
将 LOAD FILE
更改为:
LOAD DATA INFILE 'D:/TEMP/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(id,city,number,@vcomment)
SET comment = NULLIF(@vcomment,'');
我确实得到了以下结果:
MySQL [test]> LOAD DATA INFILE 'D:/TEMP/csvfile.csv'
-> INTO TABLE citytable
-> FIELDS TERMINATED BY ','
-> ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n'
-> IGNORE 1 LINES
-> (id,city,number,@vcomment)
-> SET comment = NULLIF(@vcomment,'');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
MySQL [test]> select * from citytable;
+----+------+--------+-----------+
| id | city | number | comment |
+----+------+--------+-----------+
| 1 | NY | 1 | Something |
| 2 | W | 2 | NULL |
| 3 | C | 1 | Something |
| 4 | LA | 1 | NULL |
+----+------+--------+-----------+
4 rows in set (0.00 sec)
MySQL [test]>
我正在尝试将包含一些空单元格的 CSV 文件导入 MySQL 数据库。你能帮我吗?
我的上一个线程已关闭,因为它“与 similar question 相关联”。我尝试了该解决方案但没有奏效,下面列出了对此的尝试。
我创建了一个table:
CREATE TABLE citytable (
id int(11) NOT NULL auto_increment,
city varchar(100),
number int(11) NOT NULL DEFAULT 1,
comment varchar(100),
primary key(id)
);
我已经用数据填充了一个 csv,并想将其导入 table
CSV 文件csvfile.csv
:
id,city,number,comment
1,NY,1,Something
2,W,2,
3,C,1,Something
4,LA,1,
所以我使用以下命令加载 CSV:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id,city,number,@vcomment)
SET comment = NULLIF(@vcomment,'');
我也试过将 comment
的默认值设置为 ''
并重新运行命令并得到错误 ``ERROR 1261 (01000): 第 4 行不包含所有列的数据`.
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(id,city,number,comment);
如果该单元格中没有数据,我如何只导入数据库并让值为“NULL”?
将 LOAD FILE
更改为:
LOAD DATA INFILE 'D:/TEMP/csvfile.csv'
INTO TABLE citytable
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(id,city,number,@vcomment)
SET comment = NULLIF(@vcomment,'');
我确实得到了以下结果:
MySQL [test]> LOAD DATA INFILE 'D:/TEMP/csvfile.csv'
-> INTO TABLE citytable
-> FIELDS TERMINATED BY ','
-> ENCLOSED BY '"'
-> LINES TERMINATED BY '\r\n'
-> IGNORE 1 LINES
-> (id,city,number,@vcomment)
-> SET comment = NULLIF(@vcomment,'');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
MySQL [test]> select * from citytable;
+----+------+--------+-----------+
| id | city | number | comment |
+----+------+--------+-----------+
| 1 | NY | 1 | Something |
| 2 | W | 2 | NULL |
| 3 | C | 1 | Something |
| 4 | LA | 1 | NULL |
+----+------+--------+-----------+
4 rows in set (0.00 sec)
MySQL [test]>