我已經搜索了幾個小時,但它不起作用。LOAD DATA LOCAL INFILE不能跳過列?
我有一個總統任職的csv文件。第一行是標題。
Presidency ,President ,Wikipedia Entry,Took office ,Left office ,Party ,Portrait,Thumbnail,Home State
我想創建只有Presidency
,President
和Party
的關係。
$sqlquerycreate1 = "CREATE TABLE IF NOT EXISTS `sample1` (
`Presidency` VARCHAR(30),
`President` VARCHAR(30),
`Party` VARCHAR(30)
)";
mysql_query($sqlquerycreate1);
# download the file off the internet
$file = file_get_contents("http://localhost/sample.csv");
$filepath = "sample.csv";
file_put_contents($filepath, $file);
# load the data into nycgrid table
$pt11 = "LOAD DATA LOCAL INFILE ";
$pt21 = "'C:/xampp/htdocs/test/file/sample.csv' INTO TABLE sample1(Presidency,President,@Wikipedia Entry,@Took office ,@Left office ,Party,@Portrait,@Thumbnail,@Home State) ";
$pt31 = "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ";
$pt41 = "LINES TERMINATED BY '\r\n' ";
$pt51 = "IGNORE 1 LINES";
$sqlquerynew1 = $pt11.$pt21.$pt31.$pt41.$pt51;
mysql_query($sqlquerynew1);
它不起作用。該表已創建,但未加載數據。 任何想法爲什麼?謝謝。
mysql> LOAD DATA LOCAL INFILE 'C:/xampp/htdocs/sample.csv' INTO TABLE sample1(Pr
esidency, President, @x, @x, @x, Party, @x, @x, @x) FIELDS TERMINATED BY ',' ENC
LOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'FIELD
S TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINE' at
line 1
感謝。我用你的代碼替換了'INTO TABLE ...'部分,它仍然不起作用。另外,你是否覺得'LOAD DATA LOCAL INFILE'後面不需要空格?我相信我需要它們是因爲我稍後將它們連接起來。 – user1012451
謝謝。是。我確定,因爲如果我讓它導入所有列,我可以導入。 – user1012451
謝謝。我更新了錯誤信息。太奇怪了。 – user1012451