如何使用数据库表头更新csv列名 [英] How to update csv column names with database table header

查看:153
本文介绍了如何使用数据库表头更新csv列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几天我遇到了这个问题,现在很沮丧,因为我必须这样做。

I am facing this problem some past days and now frustrate because I have to do it.

我需要用数据库表头更新我的CSV文件列名。我的数据库表字段与CSV文件不同。现在的问题是,首先我想用数据库表头更新CSV文件的列名,然后用字段映射将其数据导入数据库。

I need to update my CSV file columns name with database table header. My database table fields are different from CSV file. Now the problem is that first I want to update column name of CSV file with database table headers and then import its data with field mapping into database.

请帮帮我我不知道如何解决这个问题。

Please help me I don't know how I can solve this.

这是我的PHP代码:

$file      = $_POST['fileName'];
$filename  = "../files/" . $file;
$list      = $_POST['str'];
$array_imp = implode(',', $list);
$array_exp = explode(',', $array_imp);
$fp        = fopen("../files/" . $file, "w");
$num       = count($fp);

for ($c = 0; $c < $num; $c++) {
    if ($fp[c] !== '') {
        fputcsv($fp, $array_exp);
    }
}

fclose($fp);


require_once("../csv/DataSource.php");
$path = "../files/test_mysql.csv";
$dbtable = $ext[0];

$csv = new File_CSV_DataSource;
        $csv->load($path);
        $csvData = $csv->connect();
        $res=''; 
        foreach($csvData  as $key)
        {  print_r($key[1]);
            $myKey ='';
            $myVal='';
            foreach($key as $k=>$v)
            { 
                $myKey .=$k.',';
                $myVal .="'".$v."',";

            }
            $myKey = substr($myKey, 0, -1);
            $myVal = substr($myVal, 0, -1); 
            $query="insert into tablename($myKey)values($myVal)";
            $res=  mysql_query($query);


推荐答案

你有一个第一行的现有文件需要更换。

You have got an existing file of which the first line needs to be replaced.

这里一般概述:


  • < a href =https://stackoverflow.com/questions/235604/overwrite-line-in-file-with-php>用PHP覆盖文件中的行

  • Overwrite Line in File with PHP

一些小解释(以及其他问题未涉及的一些提示)。大多数情况下,这里使用两个文件更容易操作:

Some little explanation (and some tips that are not covered in the other question). Most often it's easier to operate with two files here:


  1. 现有文件(要复制)

  2. 暂时用于写入的新文件。

完成后,旧文件将被删除,新文件将被删除文件将重命名为旧文件的名称。

When done, the old file will be deleted and the new file will be renamed to the name of the old file.

您的代码无效,因为您已将新的第一行写入旧文件文件。当你关闭文件时,这会切断文件的其余部分。

Your code does not work because you are already writing the new first line into the old file. That will chop-off the rest of the file when you close it.

你也看起来误导了一些基本的PHP功能,例如:在文件句柄上使用 count 对您没有帮助获得行数。它只会返回1.

Also you look misguided about some basic PHP features, e.g. using count on a file-handle does not help you to get the number of lines. It will just return 1.

以下是您需要做的一步一步:

Here is step by step what you need to do:


  1. 打开要读取的现有文件。只需阅读它的第一行来推进文件指针( fgets

  2. 打开要写入的新文件。将新标题写入其中(正如您已成功完成的那样)。

  3. 将第一个文件中的所有剩余数据复制到新的第二个文件中。 PHP有一个功能,它被称为 stream_copy_to_stream

  4. 关闭这两个文件。

  1. Open the existing file to read from. Just read the first line of it to advance the file-pointer (fgets)
  2. Open a new file to write into. Write the new headers into it (as you already successfully do).
  3. Copy all remaining data from the first file into the new, second file. PHP has a function for that, it is called stream_copy_to_stream.
  4. Close both files.


现在检查新文件是否属于你正在寻找。当这一切都有效时,您需要添加更多步骤:

Now check if the new file is what you're looking for. When this all works, you need to add some more steps:


  • 将原始文件重命名为新名称。这可以通过 重命名 来完成。

    如果你想要,然后你可以删除你在5中重命名的文件。 - 但是只有当你不再需要它时。

    If you want, you then can delete the file you renamed in 5. - but only if you don't need it any longer.

    就是这样。我希望这是有帮助的。 PHP手册包含所提及和链接的所有功能的示例代码。祝你好运。如果您不了解自己的代码,请先使用手册阅读相关内容。这减少了你可以引入错误的地方。

    And that's it. I hope this is helpful. The PHP manual contains example code for all the functions mentioned and linked. Good luck. And if you don't understand your own code, use the manual to read about it first. That reduces the places where you can introduce errors.

    这篇关于如何使用数据库表头更新csv列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆