如何用PHP解析CSV文件并插入MySQL数据库?

时间:2012-11-22 03:48:55

标签: php mysql parsing csv

我有一个像这样的远程CSV:

EUR/USD,1353555876030,1.28,435,1.28,442,1.28261,1.28703,1.28278
USD/JPY,1353555897400,82.,448,82.,449,82.370,82.594,82.524
GBP/USD,1353555880620,1.59,610,1.59,618,1.59496,1.59722,1.59524
EUR/GBP,1353555883845,0.80,464,0.80,480,0.80374,0.80590,0.80416
USD/CHF,1353555869856,0.93,793,0.93,807,0.93549,0.93919,0.93886
EUR/JPY,1353555897821,105.,894,105.,901,105.825,106.267,105.862
EUR/CHF,1353555869848,1.20,470,1.20,491,1.20395,1.20509,1.20434
USD/CAD,1353555889301,0.99,580,0.99,595,0.99555,0.99684,0.99623
AUD/USD,1353555900458,1.03,892,1.03,901,1.03658,1.04019,1.03691
GBP/JPY,1353555897599,131.,593,131.,603,131.516,131.890,131.642

将所有这些信息发送到mySQL数据库的最佳方法是什么? (我想每秒刷新并更新价格)

4 个答案:

答案 0 :(得分:5)

但是,您决定将CSV内容提供给PHP,请确保使用fgetcsv()进行实际的CSV解析。

<?php

$csv = <<<EOT
EUR/USD,1353555876030,1.28,435,1.28,442,1.28261,1.28703,1.28278
USD/JPY,1353555897400,82.,448,82.,449,82.370,82.594,82.524
GBP/USD,1353555880620,1.59,610,1.59,618,1.59496,1.59722,1.59524
EUR/GBP,1353555883845,0.80,464,0.80,480,0.80374,0.80590,0.80416
USD/CHF,1353555869856,0.93,793,0.93,807,0.93549,0.93919,0.93886
EUR/JPY,1353555897821,105.,894,105.,901,105.825,106.267,105.862
EUR/CHF,1353555869848,1.20,470,1.20,491,1.20395,1.20509,1.20434
USD/CAD,1353555889301,0.99,580,0.99,595,0.99555,0.99684,0.99623
AUD/USD,1353555900458,1.03,892,1.03,901,1.03658,1.04019,1.03691
GBP/JPY,1353555897599,131.,593,131.,603,131.516,131.890,131.642
EOT;

// create a read/write location in memory
$handle=fopen("php://memory", "rw");

// copy the CSV to memory
fwrite($handle, $csv);

// rewind the handle to the beginning of the CSV
fseek($handle, 0);

// read the CSV from memory
while (($row=fgetcsv($handle)) !== false) {

    // handle each row
    print_r($row);

    // do whatever mysql operations you want here
    // ...
};

输出

Array
(
    [0] => EUR/USD
    [1] => 1353555876030
    [2] => 1.28
    [3] => 435
    [4] => 1.28
    [5] => 442
    [6] => 1.28261
    [7] => 1.28703
    [8] => 1.28278
)
Array
(
    [0] => USD/JPY
    [1] => 1353555897400
    [2] => 82.
    [3] => 448
    [4] => 82.
    [5] => 449
    [6] => 82.370
    [7] => 82.594
    [8] => 82.524
)
Array
(
    [0] => GBP/USD
    [1] => 1353555880620
    [2] => 1.59
    [3] => 610
    [4] => 1.59
    [5] => 618
    [6] => 1.59496
    [7] => 1.59722
    [8] => 1.59524
)
Array
(
    [0] => EUR/GBP
    [1] => 1353555883845
    [2] => 0.80
    [3] => 464
    [4] => 0.80
    [5] => 480
    [6] => 0.80374
    [7] => 0.80590
    [8] => 0.80416
)
Array
(
    [0] => USD/CHF
    [1] => 1353555869856
    [2] => 0.93
    [3] => 793
    [4] => 0.93
    [5] => 807
    [6] => 0.93549
    [7] => 0.93919
    [8] => 0.93886
)
Array
(
    [0] => EUR/JPY
    [1] => 1353555897821
    [2] => 105.
    [3] => 894
    [4] => 105.
    [5] => 901
    [6] => 105.825
    [7] => 106.267
    [8] => 105.862
)
Array
(
    [0] => EUR/CHF
    [1] => 1353555869848
    [2] => 1.20
    [3] => 470
    [4] => 1.20
    [5] => 491
    [6] => 1.20395
    [7] => 1.20509
    [8] => 1.20434
)
Array
(
    [0] => USD/CAD
    [1] => 1353555889301
    [2] => 0.99
    [3] => 580
    [4] => 0.99
    [5] => 595
    [6] => 0.99555
    [7] => 0.99684
    [8] => 0.99623
)
Array
(
    [0] => AUD/USD
    [1] => 1353555900458
    [2] => 1.03
    [3] => 892
    [4] => 1.03
    [5] => 901
    [6] => 1.03658
    [7] => 1.04019
    [8] => 1.03691
)
Array
(
    [0] => GBP/JPY
    [1] => 1353555897599
    [2] => 131.
    [3] => 593
    [4] => 131.
    [5] => 603
    [6] => 131.516
    [7] => 131.890
    [8] => 131.642
)
[Finished in 0.1s]

请注意,如果您在文件系统的某个位置将CSV作为文件提供,则没有理由将整个内容加载到内存中。在这种情况下,请使用fopen()并传入文件名和r参数。

答案 1 :(得分:1)

如果您有CSV文件,导入MySQL数据库的最快方法是使用LOAD DATA INFILE,例如:

LOAD DATA INFILE 'exchange_rates.csv' 
INTO TABLE exchange_rates (@var1, @var2, @var3, @var4)
SET 
    from_currency = @var1, 
    to_currency = @var2,
    exchange_rate = @var3,
    etc...

答案 2 :(得分:0)

您可以运行Cron Scheduler之类的计划作业,这些作业将获取您的CSV条目,格式化为INSERT SQL语句并触发查询。

答案 3 :(得分:0)

我建议您比较两个最新文件,只更新更改而不是一直更新整个表格