将CSV文件直接导入MySQL [英] Import CSV file directly into MySQL
本文介绍了将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中的列名称以及数据库表中的列名称是不是我应该怎么办?
i想以编程方式做。
but column names in csv and that in database table are different what should i do? i want to do it programmatically..
推荐答案
如下:
$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屋!
查看全文