简体   繁体   中英

import csv file to mysql

I am trying to load data from a csv file that is still in excel. So far this is the statement that I have as my sql query:

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 ? i wonder what im missing is it my delimeter? LINES TERMINATED BY '\\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 .

OR

Use 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. 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. It wasnt a problem with my LOAD statement . 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 . 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. Some programs, such as WordPad , might use \\r as a line terminator when writing files. To read such files, use 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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