douben1891 2010-11-10 11:24
浏览 312
已采纳

将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 '
'
(uniqName, uniqCity, uniqComments)

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

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

  • 写回答

5条回答 默认 最新

  • dqk77945 2010-11-10 11:28
    关注

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

    Something like:

        $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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器