Save CSV files into mysql database

前端 未结 3 1476
旧时难觅i
旧时难觅i 2020-12-05 22:44

I have a lot of csv files in a directory. With these files, I have to write a script that put their content in the right fields and tables of my database. I am almost beginn

3条回答
  •  执念已碎
    2020-12-05 23:06

    Also mysqlimport can be used

    private function runImport($host, $username, $password, $schema, $csvFilePath) {
        $output = array();
        $cmd = "/usr/bin/mysqlimport --host=$host";
        $cmd .= ' --fields-terminated-by=\',\'  --fields-optionally-enclosed-by=\'"\' ';
        $cmd .= ' --user=' . $username . ' --password='. $password;
        $cmd .= " $schema $csvFilePath";
    
        exec($cmd, $output, $retVal);
    
        foreach ($output as $line) {
           echo "$line\n";
        }
    
        echo "\n\nReturn code : $retVal";
    }
    

提交回复
热议问题