将 CSV 文件直接导入 MySQL [英] Import CSV file directly into MySQL

查看:38
本文介绍了将 CSV 文件直接导入 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 csv 文件导入 mysql.. 类似于:

i want to import csv file into mysql.. something like:

load data local infile 'uniq.csv' into table tblUniq
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(uniqName, uniqCity, uniqComments)

http://www.tech-recipes.com/rx/2345/import_csv_file_directly_into_mysql/

但是csv中的列名和数据库表中的列名不同我该怎么办?我想以编程方式进行..

but column names in csv and that in database table are different what should i do? i want to do it programmatically..

推荐答案

您可以创建一个脚本来解析您的 csv 文件并将数据放入 db.

You can create a script to parse your csv file and to put the data into db.

类似于:

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

        }
        fclose($handle);
    }
    // this you'll have to expand
    foreach($data_entries as $line){
        $sql = "INSERT INTO ..."
        $db->execute($line);
    }

这篇关于将 CSV 文件直接导入 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆