CSV 将列不匹配和跳过的数据加载到 mySQL

CSV Load Data into mySQL with mismatched and skipped columns

我正在尝试将数据从 csv 加载到 mySQL。列的顺序不正确,有些列被跳过。日期格式也不对

数据如下所示:

Date,Open,High,Low,Close,Adj Close,Volume 4/3/2006,10.01,10.01,10.01,10.01,7.236886,0 4/4/2006,10.07,10.07,10.07,10.07,7.280261,0

我的代码如下所示:

$query = <<<eof
    LOAD DATA INFILE '$filename'
      INTO TABLE `$table`
      FIELDS TERMINATED BY ','
      LINES TERMINATED BY '\r\n'    
      IGNORE 1 LINES
      (@date_str, @open, @high, @low, @close, @adjclose, @volume)
      SET `date` = STR_TO_DATE(@date_str, '%c/%e/%Y'),
      SET symbol = $symbol,
      SET nav = @close; 
eof;

我得到的错误是:

Sql Error: LOAD DATA INFILE 'D:/Website/finance/csv/ABNDX.csv' INTO TABLE mutual_fund_history FIELDS TERMINATED BY ',' LINES TERMINATED BY ' ' IGNORE 1 LINES (@date_str, @open, @high, @low, @close, @adjclose, @volume) SET date = STR_TO_DATE(@date_str, '%c/%e/%Y'), SET symbol = ABNDX, SET nav = @close; Sql error #: 1064 - 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 'SET symbol = ABNDX, SET nav = @close' at line 8…

我难住了。感谢您的帮助。

更新:我尝试在“$symbol”周围加上单引号,因为它是一个字符串,但仍然出现错误。

请注意,我已遵循 this post 指导我。

好的,从那个 post 得到它。只能使用一个 SET 语句,不同的变量必须用逗号分隔,因此应该是:

$query = <<<eof
    LOAD DATA INFILE '$filename'
     INTO TABLE `$table`
     FIELDS TERMINATED BY ','
     LINES TERMINATED BY '\r\n'    
     IGNORE 1 LINES
     (@date_str, @open, @high, @low, @close, @adjclose, @volume)
     SET `date` = STR_TO_DATE(@date_str, '%c/%e/%Y'),
      symbol = '$symbol',
      nav = @close;
eof;