我一直在尝试将csv文件上传到pythonanywhere上mysql数据库的表中,我无法解决为什么LOAD DATA LOCAL INFILE命令不起作用。
这是我的命令和结果:
mysql> LOAD DATA LOCAL INFILE '/twenty_year_change_data.csv'
-> INTO TABLE twenty_year_change_data
-> FIELDS TERMINATED BY ','
-> ENCLOSED BY '"'
-> LINES TERMINATED BY '\n'
-> IGNORE 1 ROWS;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Deleted: 0 Skipped: 0 Warnings: 0
我再次尝试了这个命令,但取消了删除标题:
mysql> LOAD DATA LOCAL INFILE '/twenty_year_change_data.csv'
-> INTO TABLE twenty_year_change_data
-> FIELDS TERMINATED BY ','
-> ENCLOSED BY '"'
-> LINES TERMINATED BY '\n';
Query OK, 1 row affected, 2 warnings (0.01 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 2
看警告:
mysql> show warnings;
+---------+------+-----------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------+
| Warning | 1265 | Data truncated for column 'pcode_district' at row 1 |
| Warning | 1265 | Data truncated for column 'percent_change' at row 1 |
+---------+------+-----------------------------------------------------+
2 rows in set (0.00 sec)
这是表格的样子:
mysql> select * from twenty_year_change_data;
+----------------+------+------+----------------+
| pcode_district | 1995 | 2015 | percent_change |
+----------------+------+------+----------------+
| pcode_ | 1995 | 2015 | 0 |
+----------------+------+------+----------------+
1 row in set (0.00 sec)
查看数据的格式:
mysql> describe twenty_year_change_data
-> ;
+----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| pcode_district | varchar(6) | NO | | NULL | |
| 1995 | float | YES | | NULL | |
| 2015 | float | YES | | NULL | |
| percent_change | float | YES | | NULL | |
+----------------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
我猜这可能是下划线的问题所以我调整了csv文件,所以标题不包含它们,但是在导入第一个单元格后它仍然停止。
如何解决问题,使用mysql时需要注意哪些格式?
更新:这里是csv的顶部
pcode_district,1995,2015,percent_change
AL1 1,79700,427500,436.3864492
AL1 2,78125,384250,391.84
AL1 3,66500,306500,360.9022556
AL1 4,98000,575000,486.7346939
AL1 5,72000,365250,407.2916667
答案 0 :(得分:1)
感谢@barmar的回答。问题是csv文件行被' \ r \ \而不是' \ n'终止。