单元格中的文字有','上传.csv不起作用

时间:2011-02-10 05:53:07

标签: mysql csv

我用来上传数据的.csv文件在单元格中有','。如何忽略单元格中的“,”,以便该单元格中的数据进入我的数据库的单个字段。

细胞数据=“从墙壁上剥落铅基涂料,在墙壁上生长霉菌。总结与铅和霉菌相关的健康危害,并描述可以采取哪些措施来控制这两种公共卫生问题。请一定要用文献中的证据支持你的立场“

这个文本应该放在一个字段中,但因为文本中有一个','它出现在2个字段中。

有没有人知道这方面的解决方法。

由于

PHP代码

$fieldseparator = ",";
$lineseparator = "\n";

$csvfile = "SampleDataTab.txt";
//echo $csvfile;
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
echo $csvfile;

if(!file_exists($csvfile)) {
    echo "File not found. Make sure you specified the correct path.\n";
    exit;
}

$file = fopen($csvfile,"r");

if(!$file) {
    echo "Error opening data file.\n";
    exit;
}

$size = filesize($csvfile);

if(!$size) {
    echo "File is empty.\n";
    exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = mysql_connect($databasehost,$databaseusername) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
//'/\s/',
$records=0;
$lines = 0;
$queries = "";
$query="";
$linearray = array();
//array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
foreach(preg_split('/\n/',$csvcontent) as $line) {
echo"<BR>". $line."<BR>";
    $lines++;

    $line = trim($line," \t");

    $line = str_replace("\r","",$line);

    /************************************
    This line escapes the special character. remove it if entries are already escaped in the csv file
    ************************************/
    if($lines==1)
    $line = str_replace("`","\'",$line);
else
    $line = str_replace("'","\'",$line);
    /*************************************/

    $linearray = explode($fieldseparator,$line);


    $stat=  mysql_query("select status_id from status where status='$linearray[3]'");
     $status=mysql_fetch_row($stat);


    $wri=  mysql_query("select user_id from writer where name='$linearray[4]'");
     $writer=mysql_fetch_row($wri);


    $due= date("Y-m-d H:i:s",strtotime($linearray[5]));
    $ord_date=date("Y-m-d H:i:s",strtotime($linearray[6]));

    if($writer[0]==null)
    echo "Please insert writer in database. Cannot add record";
    else
    {
    echo "<BR> category: ".$linearray[13]."<BR>";
if($lines!=1)
{
        $query = "insert into `$databasetable` (website,order_id,status,writer_id,due_date,order_recieve_date,no_of_pages,customer_name,topic,details,category,education_level,format,citation_style,email,alt_email) values('$linearray[0]','$linearray[1]',$status[0],$writer[0],'$due','$ord_date',$linearray[8],'$linearray[10]','$linearray[11]','$linearray[12]','$linearray[13]','$linearray[14]','$linearray[17]','$linearray[18]','$linearray[19]','$linearray[20]');";

    $queries .= $query . "\n";}

echo $query."<BR>";
$records+=  mysql_query($query);

echo $records;

}
}

mysql_close($con);

if($save) {

    if(!is_writable($outputfile)) {
        echo "File is not writable, check permissions.\n";
    }

    else {
        $file2 = fopen($outputfile,"w");

        if(!$file2) {
            echo "Error writing to the output file.\n";
        }
        else {
            fwrite($file2,$queries);
            fclose($file2);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

如果您使用LOAD DATA INFILE语法,请在查询中添加ENCLOSED BY '"'

文档:http://dev.mysql.com/doc/refman/5.1/en/load-data.html