1

我有一个像这样的远程 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

4 回答 4

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参数。

于 2012-11-22T03:59:12.743 回答
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...
于 2012-11-22T04:07:10.943 回答
0

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

于 2012-11-22T03:56:14.680 回答
0

您可以运行诸如Cron Scheduler之类的调度作业,它将获取您的 CSV 条目、格式化为 INSERT SQL 语句并触发查询。

于 2012-11-22T03:51:35.067 回答