简体   繁体   English

将CSV文件导入MySQL表

[英]Import CSV File Into MySQL Table

I have those line in CSV file and i wanna import those column into database 我在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

Every Line begin with this "ALARM" and my separator is "/" Please help me to import this data to a MySQL DB. 每行都以“ ALARM”开头,分隔符为“ /”。请帮助我将此数据导入到MySQL数据库中。

Please refer to the documentation for LOAD DATA INFILE here.. http://dev.mysql.com/doc/refman/5.7/en/load-data.html 请参考这里LOAD DATA INFILE的文档.. http://dev.mysql.com/doc/refman/5.7/en/load-data.html

You can use csv friendly characters in 您可以在中使用csv友好字符

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

Also there are certail GUI tools like SQLyog , which give a direct and an easy way to import CSV files partly or completely into your table on the server. 此外,还有诸如SQLyog之类的一些GUI工具,它们提供了直接,简单的方法来将CSV文件部分或全部导入到服务器上的表中。

As they suggest in the comments a better way is: 正如他们在评论中建议的那样,更好的方法是:

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

简单的方法是使用像opencsv这样的库

i have on Error : 我有错误:

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

and this is the query: 这是查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM