简体   繁体   English

导入CSV文件到MySQL

[英]import csv file to mysql

I am trying to load data from a csv file that is still in excel. 我正在尝试从仍在excel中的csv文件加载数据。 So far this is the statement that I have as my sql query: 到目前为止,这是我作为sql查询获得的语句:

LOAD DATA LOCAL INFILE 'C:\\Documents and Settings\\J03299\\Desktop\\TMETER.csv' 
INTO TABLE edata 
COLUMNS TERMINATED BY ',' ENCLOSED BY "" LINES TERMINATED BY '\n' 
(Year,Month,Day,MJD,xpiles,xstacks,Utilites); 

it imports the first row and then ignores every other row until row 157 ? 它导入第一行,然后忽略其他所有行,直到157行? i wonder what im missing is it my delimeter? 我想知道我的距离表是什么? LINES TERMINATED BY '\\n'? 以'\\ n'结尾的行? my data looks like this 我的数据看起来像这样

2012,23,45,-0.876,3.456,768.50,

somehow it is not reading the end of file i guess 我猜莫名其妙地没有读文件的结尾

is there any other tool that i can use to import this information perhaps 还有其他我可以用来导入此信息的工具吗?

you can try using Mysql workbench . 您可以尝试使用Mysql workbench

OR 要么

Use LINES TERMINATED BY '\\r\\n' 使用以LINES TERMINATED BY '\\r\\n'

solved the problem...it was my stupid mistake apparanely i was using year as the primary key when creating the table yet year is not unique. 解决了问题...这大概是我的愚蠢错误,我在创建表时使用year作为主键,但是year不是唯一的。 Only two values are represented which is year 2012 and 2013 and so it was only reading the first row with 2012 and since all subsequent rows were the same it ignored them until it got to 2013 (next unique primary key) and took the first row to thaty and then gave up again. 仅表示两个值,分别是2012年和2013年,因此它仅读取2012年的第一行,并且由于所有后续行都相同,因此忽略它们直到到达2013年(下一个唯一主键),然后将第一行移至那然后又放弃了。 It wasnt a problem with my LOAD statement . 我的LOAD语句不是问题。 Thank everyone for assisting me andi should have done better when creating the database table 谢谢大家的帮助,并且在创建数据库表时我应该做得更好

On Windows, line termination is usually the carriage return character U+000d followed by the linefeed character U+000a . 在Windows上,行终止通常是回车符U + 000d,然后是换行符U + 000a As the manual states: 手册所述:

Note 注意

If you have generated the text file on a Windows system, you might have to use LINES TERMINATED BY '\\r\\n' to read the file properly, because Windows programs typically use two characters as a line terminator. 如果在Windows系统上生成了文本文件,则可能必须使用LINES TERMINATED BY '\\r\\n'来正确读取该文件,因为Windows程序通常使用两个字符作为行终止符。 Some programs, such as WordPad , might use \\r as a line terminator when writing files. 某些程序(例如WordPad在写入文件时可能使用\\r作为行终止符。 To read such files, use LINES TERMINATED BY '\\r' . 要读取此类文件,请使用LINES TERMINATED BY '\\r'

Therefore: 因此:

LOAD DATA LOCAL INFILE 'C:\Documents and Settings\J03299\Desktop\TMETER.csv'
    INTO TABLE edata
    COLUMNS
        TERMINATED BY ','
        ENCLOSED BY ''
    LINES
        TERMINATED BY '\r\n'
    (Year, Month, Day, MJD, xpiles, xstacks, Utilites)

The issue is that your SQL query has: 问题是您的SQL查询具有:

LINES TERMINATED BY '\n' 

yet your data is terminated by ',' ( a comma). 但是您的数据以','(逗号)结尾。

2012,23,45,-0.876,3.456,768.50,

Try changing that line to something like: 尝试将该行更改为以下内容:

 LINES TERMINATED BY ',\n' 

The other alternative is to clean-up the input data by removing the trailing comma. 另一种选择是通过删除尾随逗号来清理输入数据。

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

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