MySQL CSV文件导入dateTime

时间:2014-10-22 17:18:14

标签: mysql datetime csv file-io load

新来的 - 友善。

我正在将CSV文件导入到mysql表中,并且日期时间类型特别困难。

我的导入示例,日期时间值将是第4个字段。

3026930,{5849EF5A-210E-497D-948D-0006687AD627},550000,28/01/2013          00:00,1,1,2013,2013/1,SE145DS,S,N,F,3, ,NEW CROSS ROAD, ,LONDON,LEWISHAM,GREATER LONDON,     ,SE145DS,Inner,E09000023,Lewisham,E05000449,New Cross,E02000655,E01003302,E00016694 

我的导入声明:

    LOAD DATA INFILE '/tmp/house1.csv'
          INTO TABLE housesales
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
 LINES TERMINATED BY '\n'
              IGNORE 1 ROWS
                 SET dateprocessed = STR_TO_DATE(@dateprocessed, '%d/%m/%Y %H:%i');

我的桌子:

+----------------+------------------+------+-----+---------+-------+
| Field          | Type             | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+-------+
| saleid         | int(10) unsigned | NO   | PRI | NULL    |       |
| transactionID  | char(50)         | NO   |     | NULL    |       |
| price          | int(10) unsigned | NO   |     | NULL    |       |
| dateprocessed  | datetime         | NO   |     | NULL    |       |
| quarter        | tinyint(4)       | NO   |     | NULL    |       |
| month          | tinyint(4)       | NO   |     | NULL    |       |
| year           | smallint(6)      | NO   |     | NULL    |       |
| yearmonth      | char(10)         | NO   |     | NULL    |       |
| postcode       | char(10)         | NO   |     | NULL    |       |
| propertytype   | char(3)          | NO   |     | NULL    |       |
| whethernew     | char(3)          | NO   |     | NULL    |       |
| tenure         | char(3)          | NO   |     | NULL    |       |
| address1       | char(50)         | YES  |     | NULL    |       |
| address2       | char(50)         | YES  |     | NULL    |       |
| address3       | char(50)         | YES  |     | NULL    |       |
| address4       | char(50)         | YES  |     | NULL    |       |
| town           | char(20)         | YES  |     | NULL    |       |
| localauthority | char(30)         | YES  |     | NULL    |       |
| county         | char(30)         | YES  |     | NULL    |       |
| recordstatus   | char(2)          | YES  |     | NULL    |       |
| postcodeclean  | char(10)         | YES  |     | NULL    |       |
| innerouter     | char(10)         | YES  |     | NULL    |       |
| boroughcode    | char(20)         | YES  |     | NULL    |       |
| boroughname    | char(30)         | YES  |     | NULL    |       |
| wardcode       | char(20)         | YES  |     | NULL    |       |
| wardname       | char(30)         | YES  |     | NULL    |       |
| msoa11         | char(20)         | YES  |     | NULL    |       |
| lsoa11         | char(20)         | YES  |     | NULL    |       |
| oa11           | char(20)         | YES  |     | NULL    |       |
+----------------+------------------+------+-----+---------+-------+

最后是导入表中的一行:

| 3118826 | {F3F11295-2C5B-44E5-870A-ED719FFB12C9} | 100500 | 0000-00-00 00:00:00 |2 |     5 | 2013 | 2013/5    | E16 1BY  | F            | N          | L      | OCEANIS APARTMENTS, 19     | FLAT 38               | SEAGULL LANE            |                      | LONDON               | NEWHAM                 | GREATER LONDON |              | E16 1BY       | Inner      | E09000025   | Newham               | | E05000478 | Canning Town South             | E02000747 | E01033576 | E00174967

如果datetime列像值一样变空,我也会超出范围警告。这可能是过多的空白吗?

谢谢 迈克尔

1 个答案:

答案 0 :(得分:0)

Does your csv file have a header? If it has one just remove it and try again.

I tried this command that I saw in another post and it worked perfectly:

 LOAD DATA LOCAL INFILE ' test.csv'
INTO TABLE  housesales
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
(row1,row2,row3,etc...)