在MySql表行中插入整个CSV文件

时间:2015-02-13 04:28:43

标签: php mysql csv

我正在尝试将CS​​V文件的所有内容存储到表格行中。

表格结构:

Field   Type   Comment
id      bigint(15) NOT NULL  Primary key for table
file_id bigint(15) NULL      Reference key from filemapper table
file_content longtext NULL    Content of the File
dataTime  timestamp NOT NULL

当我尝试使用CSV文件在此表中插入时出现问题,我收到错误" MySQL服务器已经消失 "

我没有尝试将csv文件字段拆分为mysql表中的列

有人可以提出另一种方法来做到这一点而不会导致错误吗?

2 个答案:

答案 0 :(得分:2)

MySQL server has gone away has foolwing causes and sollutions.
1.Server time out and closed the connection.To fix,check that "wait_timeout" mysql variable in your my.conf Coonfiguration file is large enough or not.
2.Server dropped an incorrect or too large pocket.If mysqld gets a packet that is too large or incorrect it assumes that something gone wrong with connection and connection closed.To fix this problem increase maximal packet size limit "max_allowed_packet" in my.conf file e.g. set  max_allowed_packet = 128M 
You Can GO to this link : http://stackoverflow.com/questions/12425287/mysql-server-has-gone-away-when-importing-large-sql-file

答案 1 :(得分:0)

为了修复这个问题,你必须增加最大数据包大小限制

MAX_ALLOWED_PACKET

在.cnf文件中,例如:

max_allowed_packet = 128M

重新启动

sudo /etc/init.d/mysql restart

使用 LOAD DATA INFILE 即可。

您需要设置字段TERMINATED BY

FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''

您的语法应如下所示

LOAD DATA INFILE ‘path/to/example.csv’ INTO TABLE example FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ IGNORE 1 LINES ;

希望这适合你。