天天看点

csv导入mysql ignore_将CSV导入mysql表

这是一个简单的PHP命令行脚本,可以满足您的需求:

$host = 'localhost';

$user = 'root';

$pass = '';

$database = 'database';

$db = mysql_connect($host, $user, $pass);

mysql_query("use $database", $db);

// Parameters: filename.csv table_name

$argv = $_SERVER[argv];

if($argv[1]) { $file = $argv[1]; }

else {

echo "Please provide a file name\n"; exit;

}

if($argv[2]) { $table = $argv[2]; }

else {

$table = pathinfo($file);

$table = $table['filename'];

}

// Get the first row to create the column headings

$fp = fopen($file, 'r');

$frow = fgetcsv($fp);

foreach($frow as $column) {

if($columns) $columns .= ', ';

$columns .= "`$column` varchar(250)";

}

$create = "create table if not exists $table ($columns);";

mysql_query($create, $db);

// Import the data into the newly created table.

$file = $_SERVER['PWD'].'/'.$file;

$q = "load data infile '$file' into table $table fields terminated by ',' ignore 1 lines";

mysql_query($q, $db);

?>

它将基于第一行创建一个表,并将剩余的行导入其中。这是命令行语法:

php csv_import.php csv_file.csv table_name