如何正确地将* .csv文件中的数据导入mysql数据库中的给定表

时间:2016-03-30 20:09:46

标签: php mysql csv

我有一个.csv文件,其行如下:

16.11.2015 00:00:01.118,0.94674,0.94641,1000000.0000,1750000.0000

现在我需要将mysql数据库的值导入到包含9个cols的表中,所以:

col1 - 16.11.2015 col2 - 00 col3 - 00 col4 - 01 col5 - 118 col6 - 0.94674 col7 - 0.94641 col8 - 1000000.0000 col9 - 1750000.0000

有人知道如何分离值f.e.使用标准命令: 加载数据infile' file.csv'表格名称字段由',';

终止

我试图在mysql知识库上寻找一些东西,但没有结果,这将是有效的...谢谢

3 个答案:

答案 0 :(得分:0)

你在找这样的东西吗?

 $file_handle = fopen($file, "r"); //opens CSV
    while (($row = fgetcsv($file_handle, 1000, ",")) !== false){
           ...code..
    }

http://php.net/manual/fr/function.fgetcsv.php

答案 1 :(得分:0)

i'd like to a direct mysql command if it is possible? i'd like to load the csv file directly to the DB with LOAD command.

答案 2 :(得分:0)

Have you looked at http://dev.mysql.com/doc/refman/5.7/en/load-data.html?

Also, some database manager/tools have the ability to import CSV files. I'm using Sequel Pro and it has the ability to do so.