将CSV文件导入MySQL表

时间:2016-02-23 08:42:40

标签: java mysql csv opencsv

我在CSV文件中有这些行,我想将这些列导入数据库

Notification Identifier / State / Severity / Event Time / Probable Cause / Managed Object / Alarm Type / Additional Text 
ALARM /  1347413987 /  Outstanding /  Critical /  Thu, Feb 18, 2016 04:50:22 PM /  NeighborDown /  5620SAM tunisiana_ns:.network.ne.5620SAM6_1 SERVICE 
service-652:192.168.161.36:routingInstance:ospf-v2:areaSite-0.0.0.0:interface-2 /  EquipmentAlarm / "PE9036

每一行都以此“ALARM”开头,我的分隔符为“/” 请帮我将这些数据导入MySQL数据库。

4 个答案:

答案 0 :(得分:2)

请参阅此处的LOAD DATA INFILE文档。http://dev.mysql.com/doc/refman/5.7/en/load-data.html

您可以在

中使用csv友好字符
 FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\'
 LINES TERMINATED BY '\n' STARTING BY ''

还有像SQLyog这样的GUI工具,可以直接,轻松地将CSV文件部分或完全导入到服务器上的表格中。

答案 1 :(得分:0)

正如他们在评论中提出的更好的方法是:

String esquel = " LOAD DATA LOCAL INFILE '" + path +
                "' INTO TABLE tableName " +
                " FIELDS TERMINATED BY \'/\' +
                " LINES TERMINATED BY \'\\n\'";

答案 2 :(得分:0)

简单的方法是使用像opencsv

这样的库

答案 3 :(得分:0)

我有错误:

ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '+' at line 1

这是查询:

 query = " LOAD DATA LOCAL INFILE '" + "D:\\alarm.csv" + "' INTO TABLE alarm " +" FIELDS TERMINATED BY \'/\' + ";