将CSV数据导入MySQL

时间:2012-02-09 13:02:14

标签: php csv import

请考虑以下来自“NASDAQ.csv”的片段CSV数据

"Symbol,""Name"",""LastSale"",""MarketCap"",""ADR TSO"",""IPOyear"",""Sector"",""industry"",""Summary Quote"",";;
"FLWS,""1-800 FLOWERS.COM, Inc."",""2.9"",""81745200"",""n/a"",""1999"",""Consumer Services"",""Other Specialty Stores"",""http://www.nasdaq.com/symbol/flws"",";;
"FCTY,""1st Century Bancshares, Inc"",""4"",""36172000"",""n/a"",""n/a"",""Finance"",""Major Banks"",""http://www.nasdaq.com/symbol/fcty"",";;
"FCCY,""1st Constitution Bancorp (NJ)"",""8.8999"",""44908895.4"",""n/a"",""n/a"",""Finance"",""Savings Institutions"",""http://www.nasdaq.com/symbol/fccy"",";;

我正在尝试将Symbol,Sector和Industry导入到具有相应字段的MySQL表中:

$path = "NASDAQ.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $row++;
    $entries[] = $data ;
  }
  fclose($handle);
}

foreach ($entries as $line) {
  db_query("
     INSERT INTO us_stocks (symbol, name, sector, industry) 
     VALUES ('%s', '%s', '%s', '%s', '%s')",
     $line[0], $line[1], $line[6], $line[7]
  );
}

然而,结果并非我的预期。在数据库中,只有符号字段被填充,甚至没有正确填写:

symbol      name  sector  industry
----------------------------------
Symbol,"Na
FLWS,"1-80
FCTY,"1st
FCCY,"1st

我做错了什么?

[编辑]

如果我是print_r($ entries),输出看起来像

Array (
  [0] => Array(
    [0] => Symbol,"Name","LastSale","MarketCap","ADR TSO","IPOyear","Sector","industry","Summary Quote",;;
  )
  [1] => Array(
    [0] => FLWS,"1-800 FLOWERS.COM, Inc.","2.9","81745200","n/a","1999","Consumer Services","Other Specialty Stores","http://www.nasdaq.com/symbol/flws",;;
  )
  [2] => Array(
    [0] => FCTY,"1st Century Bancshares, Inc","4","36172000","n/a","n/a","Finance","Major Banks","http://www.nasdaq.com/symbol/fcty",;;
  )
)

[EDIT2]

我已根据建议删除了CSV的第一行。我现在有一种快速而肮脏的方式来完成我想要的东西。基本上,每当公司名称中包含“,Inc”时,事情就会变得混乱。所以我只是将它“粘合”到上面的名称:$ data [1] = $ data [1]。 $数据[2]:

$path = "NASDAQ.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ";;")) !== FALSE) {
    if ($row < 100) {
      $row++;
      $data = explode(',', $data[0]);
      if (substr($data[2], 0, 1) == ' ') {
        $data[1] = $data[1] . $data[2];
        unset($data[2]);
      }
      $entries[] = $data ;
    }
  }
  fclose($handle);
}

print_r($ entries)现在给出:

[0] => Array
    (
        [0] => FLWS
        [1] => "1-800 FLOWERS.COM Inc."
        [3] => "2.9"
        [4] => "81745200"
        [5] => "n/a"
        [6] => "1999"
        [7] => "Consumer Services"
        [8] => "Other Specialty Stores"
        [9] => "http://www.nasdaq.com/symbol/flws"
        [10] => 
    )

最后一个问题:我不知道如何重新编号。所以3分为2分,4分为3分等,以便输出如下:

[0] => Array
    (
        [0] => FLWS
        [1] => "1-800 FLOWERS.COM Inc."
        [2] => "2.9"
        [3] => "81745200"
        [4] => "n/a"
        [5] => "1999"
        [6] => "Consumer Services"
        [7] => "Other Specialty Stores"
        [8] => "http://www.nasdaq.com/symbol/flws"
        [9] => 
    )

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

我说数据不是“真正”的CSV。

“FLWS”,“1-800 FLOWERS.COM,Inc。”“,”“2.9”“, 应该 : “FLWS”,“1-800 FLOWERS.COM,INC。”,“2.9” - 引号应用逗号分隔每个字段来包裹各个字段。通常数字字段不会被包装。

根据您加载数据的方式,数据中的逗号可能会混淆它。 (即FLOWERS.COM,INC“

顺便说一句 - 如果它真的是CSV - 请查看:http://dev.mysql.com/doc/refman/5.1/en/load-data.html

答案 1 :(得分:1)

正如Crontab所说,可能是报价问题。尝试:

foreach ($entries as $line) {

  // Escape (see mysql_real_escape_string too) and remove double quotes
  foreach ($line as $k => $v) $line[$k] = mysql_escape_string(trim($v, '"'));

  // Rebuild array
  $line = array_values($line);

  db_query("
    INSERT INTO us_stocks (symbol, name, sector, industry) 
    VALUES ('%s', '%s', '%s', '%s', '%s')",
    $line[0], $line[1], $line[6], $line[7]
 );

}

PS:我不知道你是否已经在db_query()中转义字符串。