2013-07-29 86 views
0

你好人只是想問一下關於mysqldump。 我如何插入一個csv值在一個現有的MySQL表中沒有值內? 因爲我有我的.csv文件包含兩列。 ID和NAME,我有一張有3列的表格。一個是自動遞增的ID,另一個是brgy_name,最後是brgy_code。在我的csv中,ID列值應該放在brgy_code中,而NAME列值應該放在brgy_name中。在現有的mysql表中插入新的表信息?

Example: 

"id","name" 
"1001","Bangued" 
"1002","Boliney" 
"1003","Bucay" 
"1004","Bucloc" 
"1005","Daguioman" 
"1006","Danglas" 
"1007","Dolores" 
"1008","La Paz" 
"1009","Lacub" 
"1010","Lagangilang" 
"1011","Lagayan" 
"1012","Langiden" 
"1013","Licuan-Baay" 
"1014","Luba" 
"1015","Malibcong" 
"1016","Manabo" 
"1017","Penarrubia" 
"1018","Pidigan" 
"1019","Pilar" 
"1020","Sallapadan" 
"1021","San Isidro" 
"1022","San Juan" 
"1023","San Quintin" 
"1024","Tayum" 
"1025","Tineg" 
"1026","Tubo" 
"1027","Villaviciosa" 
"1028","Butuan City" 
"1029","Buenavista" 
"1030","Cabadbaran" 
"1031","Carmen" 
. 
. 
. 

在我的表裏面沒有行。這裏是我的表結構:

CREATE TABLE `ref_barangay` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, 
    `brgy_name` VARCHAR(50) NOT NULL, 
    `brgy_code` VARCHAR(50) NOT NULL, 
    PRIMARY KEY (`id`) 
) 
COLLATE='latin1_swedish_ci' 
ENGINE=InnoDB; 

我的問題是我如何可以插入我的csv文件?在我的csv文件中,列標識是brgy_code。

Here's the what i want to do: 
id brgy_name  brgy_code 
1  Bangued   1001 
2  Boliney   1002 
. 
. 
. 

這是所有球員的我該怎麼辦呢?我想使用mysql轉儲,但我該怎麼辦?

回答

1

嘗試LOAD DATA INFILE

LOAD DATA INFILE '/path/to/your/file.csv' 
INTO TABLE ref_barangay 
    FIELDS TERMINATED BY ',' 
      OPTIONALLY ENCLOSED BY '"' 
    LINES TERMINATED BY '\n' -- or \r\n 
    IGNORE 1 LINES 
(@code, @name) 
SET brgy_name = @name, brgy_code = @code 

輸出

 
mysql> select * from ref_barangay; 
+----+--------------+-----------+ 
| id | brgy_name | brgy_code | 
+----+--------------+-----------+ 
| 1 | Bangued  | 1001  | 
| 2 | Boliney  | 1002  | 
| 3 | Bucay  | 1003  | 
| 4 | Bucloc  | 1004  | 
... 
+0

好吧,我這樣做,但是LOAD DATA INFILE '/path/to/your/file.csv' 輸出不正確。它總是走向另一條路。我檢查了我的路徑,它是正確的。 – rochellecanale

+0

這裏的infile的 'C:/database/barangay_list.csv' 的errormysql>載荷數據 - >成表ref_barangay - >字段由終止 '' - >任選地被「' 封閉 - 由'終止>線\ n' - >忽略1行 - >(@ code,@ name) - > set brgy_name = @name,brgy_code = @code; 錯誤1062(23000):重複鍵'PRIMARY'的條目'0' mysql> select * from ref_barangay; 空集(0.00秒) – rochellecanale

+0

嘗試將'LINES TERMINATED'的行分隔符從'\ n'更改爲'\ r \ n',正如我在評論中提到的 – peterm