我在将csv插入数据库时遇到问题。这是我的csv内容样本:
gender,age,name,location
male,21,jayvz,spl
female,21,jacyn,srl
这是表的表结构:
我需要做的是将csv中的数据插入到该表中,如下所示:
预期产出:
id | contact_id | label | value
1 | 1 | gender | male
2 | 1 | age | 21
3 | 1 | name | jayvz
4 | 1 | location | spl
5 | 2 | gender | female
6 | 2 | age | 21
7 | 2 | name | jacyn
8 | 2 | location | srl
(抱歉预期的输出)
任何想法?或者这甚至可能吗?
CODE:
$headers = exec("head -n 1 {$paramfile}");
$param_names = array_filter(explode(",", $headers));
$param_count = count($param_names);
$contact_count = count($contacts);
$contactsfile = getcwd()."/uploads/".date('ymdhis').".cntctid";
$row = '';
$str_csv = array();
$tmp_handler = fopen($datafile, 'r');
$param_value = '';
while(($upload_str = fgets($tmp_handler, 4096)) !== FALSE){
$param_value = explode(",", $upload_str);
array_shift($param_value); // msisdn bb
$str_line = implode(",", $param_value);
//$age = array_shift($param_value);
//$row.= implode(",", $param_value);
//for($x = 0; $x < count($param_value); ++$x){
// $row.= $param_value[$x].",";
//}
$str_csv[] = str_getcsv($str_line, ",");
}
for($a = 0; $a < $param_count; ++$a){
for($i = 0; $i < $contact_count; ++$i){
$row = $contacts[$i].",".$param_names[$a].",'<the values should be here>'";
exec("echo '".$row."' >> ".$contactsfile);
}
}
$load_query = "LOAD DATA LOCAL INFILE '{$contactsfile}' INTO TABLE {$this->contact_details} ";
$load_query .= "FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' ";
$load_query .= "(contact_id, label_name, label_value)";
$this->db->query($load_query);
return $this->db->affected_rows() > 0;
答案 0 :(得分:2)
试试这个:
$row = 1;
$labels = array();
if (($handle = fopen("your.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row==1){
$labels = $data;
} else {
foreach ($data as $key=>$value) {
$query = "insert into table values (null,$row,'{$labels[$key]}','$value')";
$this->db->query($query)
}
}
$row++;
}
fclose($handle);
}
答案 1 :(得分:1)
为什么不使用php为你解析它?更多的控制,更少担心IMO。
为此,您可以将其拆分为线条,然后将线条拆分为字段。以下假设您在字符串变量中提供了CSV,并且CSV是在您要解析它的同一系统上创建的(由于我使用了PHP_EOL常量)
$csv
$lines = explode(PHP_EOL, $csv);
$load_query = "INSERT INTO table_name (contact, label, value) VALUES ";
for($i = 1 ; $i < count($lines) ; $i++){
// Split the line into fields
$fields = $explode(",", $lines[$i]);
// Concatenate the insert values
$load_query .= "({$field[0]},{$field[1]},{$field[2]})";
// If there are more lines add a comma
if($i < (count($lines)-1)){
$load_query .= ",";
}
}
// Run the query and return
$this->db->query($load_query);
return $this->db->affected_rows() > 0;
这些字段应该真正消毒或者其他东西。
答案 2 :(得分:0)
这个问题解决了,我把数据写成了这种格式的csv文件
1,年龄,21 1,名称,jayvz 1,位置,声压级 2,年龄21 2,名称,jacyn 2,位置,SRL
然后我使用mysql的加载本地infile查询来防止内存耗尽,对于非常大的文件,我使用bash脚本的'split'将其分成多个文件
感谢大家的想法。 =)