我正在开发一个新项目,一个PHP中的Web应用程序,需要用户输入可能很大的数据集。我的解决方案是让用户上传一个csv文件,然后使用LOAD DATA INFILE
命令将其添加到我的mysql数据库。
我的测试csv:
NUMBER,ADDRESS,COMMENT,SCOUTMASTER_NAME,SCOUTMASTER_EMAIL,COMMITTEE_NAME,COMMITTEE_EMAIL,DISTRICT
4,404 test st.,so cool,bob jones,bob@jones.com,tim hanson,tim@hanson.net,western trails
5,404 test st.,so cool,bob jones,bob@jones.com,tim hanson,tim@hanson.net,western trails
6,404 test st.,so cool,bob jones,bob@jones.com,tim hanson,tim@hanson.net,western trails
7,404 test st.,so cool,bob jones,bob@jones.com,tim hanson,tim@hanson.net,western trails
DESCRIBE
表上TROOPS
命令的结果:
+---------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+-------+
| NUMBER | int(11) | NO | PRI | 0 | |
| ADDRESS | varchar(255) | YES | | NULL | |
| COMMENT | varchar(2048) | YES | | NULL | |
| SCOUTMASTER_NAME | varchar(255) | YES | | NULL | |
| SCOUTMASTER_EMAIL | varchar(255) | YES | | NULL | |
| COMMITTEE_NAME | varchar(255) | YES | | NULL | |
| COMMITTEE_EMAIL | varchar(255) | YES | | NULL | |
| PRIMARY_ELENGOMAT | int(11) | YES | | NULL | |
| SECONDARY_ELENGOMAT | int(255) | YES | | NULL | |
| DISTRICT | varchar(255) | YES | | NULL | |
| ELECTION_DATE | date | YES | | NULL | |
| MEMBERS | int(11) | YES | | NULL | |
+---------------------+---------------+------+-----+---------+-------+
相关的PHP:
$query = "LOAD DATA LOCAL INFILE '$uploadedFile'
INTO TABLE TROOPS
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(NUMBER,ADDRESS,COMMENT,SCOUTMASTER_NAME,
SCOUTMASTER_EMAIL,COMMITTEE_NAME,COMMITTEE_EMAIL,DISTRICT) ";
$result = $db->query($query);
SELECT
命令的输出(注意:这正是控制台输出,带有奇怪的空格):
+--------+--------------+---------+------------------+-------------------+----------------+-----------------+-------------------+---------------------+-----------------+---------------+---------+
| NUMBER | ADDRESS | COMMENT | SCOUTMASTER_NAME | SCOUTMASTER_EMAIL | COMMITTEE_NAME | COMMITTEE_EMAIL | PRIMARY_ELENGOMAT | SECONDARY_ELENGOMAT | DISTRICT | ELECTION_DATE | MEMBERS |
+--------+--------------+---------+------------------+-------------------+----------------+-----------------+-------------------+---------------------+-----------------+---------------+---------+
| 0 | 404 test st. | so cool | bob jones | bob@jones.com | tim hanson | tim@hanson.net | NULL | NULL | western trails | NULL | NULL |
| NULL | NULL | cool | bob jones | bob@jones.com | tim hanson | tim@hanson.net | NULL | NULL | western trails
| NULL | NULL | cool | bob jones | bob@jones.com | tim hanson | tim@hanson.net | NULL | NULL | western trails
| NULL | NULL | cool | bob jones | bob@jones.com | tim hanson | tim@hanson.net | NULL | NULL | western trails
| 7 | 404 test st. | so cool | bob jones | bob@jones.com | tim hanson | tim@hanson.net | NULL | NULL | western trails | NULL | NULL |
+--------+--------------+---------+------------------+-------------------+----------------+-----------------+-------------------+---------------------+-----------------+---------------+---------+
错误和警告表都是空的。
我的问题是,“为了正确导入这些数据,我需要调整哪些内容?”
对我来说很明显,这里发生了一些奇怪的事情,尤其是不可空的主键'number'是如何为空,以及测试字符串'如此酷'如何在三个输入的中间切断。我猜测它有换行符的东西,我为LINES TERMINATED BY
参数尝试了\ n,\ r,\ r \ n和\ n \ r。