2016-07-03 89 views
0

我有這樣的表:跳過與LOAD DATA INFILE第一列

mysql> show create table final\G; 
*************************** 1. row *************************** 
     Table: final 
Create Table: CREATE TABLE `final` (
    `id` int(4) NOT NULL AUTO_INCREMENT, 
    `cdatetime` varchar(255) NOT NULL, 
    `address` varchar(255) NOT NULL, 
    `district` varchar(255) NOT NULL, 
    `beat` varchar(255) NOT NULL, 
    `grid` varchar(255) NOT NULL, 
    `crimedescr` varchar(255) NOT NULL, 
    `ucr_ncic_code` varchar(255) NOT NULL, 
    `latitude` varchar(255) NOT NULL, 
    `longitude` varchar(255) NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
1 row in set (0.00 sec) 

而且我有CSV文件看起來像這樣:

cdatetime,address,district,beat,grid,crimedescr,ucr_ncic_code,latitude,longitude 
1/1/06 0:00,3108 OCCIDENTAL DR,3,3C  ,1115,10851(A)VC TAKE VEH W/O OWNER,2404,38.55042047,-121.3914158 
1/1/06 0:00,2082 EXPEDITION WAY,5,5A  ,1512,459 PC BURGLARY RESIDENCE,2204,38.47350069,-121.4901858 
1/1/06 0:00,4 PALEN CT,2,2A  ,212,10851(A)VC TAKE VEH W/O OWNER,2404,38.65784584,-121.4621009 
1/1/06 0:00,22 BECKFORD CT,6,6C  ,1443,476 PC PASS FICTICIOUS CHECK,2501,38.50677377,-121.4269508 

我想要做的就是載荷CSV文件放入表格最後。問題是,csv文件沒有ID列,所以我想是否有可能以某種方式告訴mysql跳過列ID並將數據加載到其餘列,但ID必須使用。所以理想情況下它應該是這樣的:

「1/1/06 0:00,3108 OCCIDENTAL DR,3,3C,1115,10851(A)VC TAKE VEH W/O OWNER,2404,38.55042047,-121.3914158 「被加載到列和MySQL自動添加1列ID,然後」1/1/06 0:00,2082 EXPEDITION WAY,5,5A,1512,459 PC BURGLARY RESIDENCE,2204,38.47350069,-121.4901858「被加載和MySQL的加入2至ID列等等等等。

最近用戶「陰影」告訴我,我應該指定我要加載的列,所以我做了這樣的事情:

load data infile '/SacramentocrimeJanuary2006.csv' INTO TABLE final (cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude); 

Mysql的返回:

ERROR 1261 (01000): Row 1 doesn't contain data for all columns 

根據mysql負載數據infile手動字段分隔符不是「,」所以我試圖通過在我的語句結尾添加FIELDS TERMINATED BY','來改變它,但這打破了查詢。這裏的正確語法是什麼?

感謝

ANSWER

mysql> CREATE TABLE `final` (
    -> `id` int(4) NOT NULL AUTO_INCREMENT, 
    -> `cdatetime` longtext NULL, 
    -> `address` longtext NULL, 
    -> `district` longtext NULL, 
    -> `beat` longtext NULL, 
    -> `grid` longtext NULL, 
    -> `crimedescr` longtext NULL, 
    -> `ucr_ncic_code` longtext NULL, 
    -> `latitude` longtext NULL, 
    -> `longitude` longtext NULL, 
    -> PRIMARY KEY (`id`) 
    ->) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
Query OK, 0 rows affected (0.17 sec) 

mysql> LOAD DATA infile '/SacramentocrimeJanuary2006.csv' INTO TABLE final FIELDS TERMINATED BY ',' lines terminated by '\r' IGNORE 1 ROWS (cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude); 
Query OK, 7584 rows affected (0.08 sec) 
Records: 7584 Deleted: 0 Skipped: 0 Warnings: 0 

回答

1

的Linux:

LOAD DATA INFILE '/home/frank/try_this123.txt' 
INTO TABLE final 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES 
(cdatetime, address,district,beat,grid,crimedescr,ucr_ncic_code,latitude,longitude) 
set id = NULL; 

或Windows:

LOAD DATA INFILE 'c:\\nate\\try_this123.txt' 
INTO TABLE final 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\r\n' 
IGNORE 1 LINES 
(cdatetime, address,district,beat,grid,crimedescr,ucr_ncic_code,latitude,longitude) 
set id = NULL; 

mysql> select * from final; 
+----+-------------+---------------------+----------+------------+------+-------------------------------+---------------+-------------+---------------+ 
| id | cdatetime | address    | district | beat  | grid | crimedescr     | ucr_ncic_code | latitude | longitude  | 
+----+-------------+---------------------+----------+------------+------+-------------------------------+---------------+-------------+---------------+ 
| 1 | 1/1/06 0:00 | 3108 OCCIDENTAL DR | 3  | 3C   | 1115 | 10851(A)VC TAKE VEH W/O OWNER | 2404   | 38.55042047 | -121.3914158 
| 2 | 1/1/06 0:00 | 2082 EXPEDITION WAY | 5  | 5A   | 1512 | 459 PC BURGLARY RESIDENCE | 2204   | 38.47350069 | -121.4901858 
| 3 | 1/1/06 0:00 | 4 PALEN CT   | 2  | 2A   | 212 | 10851(A)VC TAKE VEH W/O OWNER | 2404   | 38.65784584 | -121.4621009 
| 4 | 1/1/06 0:00 | 22 BECKFORD CT  | 6  | 6C   | 1443 | 476 PC PASS FICTICIOUS CHECK | 2501   | 38.50677377 | -121.4269508 
+----+-------------+---------------------+----------+------------+------+-------------------------------+---------------+-------------+---------------+ 

我得到了這個工作,沒有像單引號或雙引號封閉的分界。問題是,當你的地址有逗號時,會發生什麼情況,並且它會拋出所有數據並帶來一個不斷變化的問題。

這就是爲什麼,最好(讀:幾乎絕對的),你需要裹在一般的雙引號的數據,除非是由您生成的數據,幾乎簡單,如:

1,2,cat,14,8 

所以,在當第三方系統無法控制數據的輸入方式時,人們必須先編寫ETL例程來清理數據,以便使用足夠的故障安全包裝爲導入數據做好準備。

1

我想你需要添加enclosed byignore rows指令

LOAD DATA infile '/SacramentocrimeJanuary2006.csv' 
INTO TABLE final 
(cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude); 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '' 
IGNORE 1 ROWS; 
+0

的MySQL> LOAD DATA INFILE '/SacramentocrimeJanuary2006.csv' - ENCLOSED BY '> INTO TABLE TERMINATED BY最後場 ''' IGNORE 1個ROWS - >(cdatetime ,地址,地區,節拍,網格,犯罪記錄,ucr_ncic_code,緯度,經度); 查詢OK,0行受影響(0.00秒) **記錄:0刪除:0跳過:0警告:0 ** – jonasv

1

使用以下格式:

load data infile '/SacramentocrimeJanuary2006.csv' INTO TABLE final (cdatetime, address, district, beat, grid, crimedescr, ucr_ncic_code, latitude, longitude) 
fields terminated by ',' 
lines terminated by '\r\n' 
ignore 1 lines;