将csv导入mysql期间的空字段

0sgqnhkj  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(315)

我必须使用php脚本导入mysql db表中的csv文件。csv文件如下:

"giorno","lmedollton","changedolleuro","euroton","lmesterton","delnotiz","girm","sgm"
"2016-09-02", "100.01", "4005.09", "5000", "1.09", "120.09", "100.5", "200.77"
"2016-09-03", "150.01", "4205.09", "5600", "1.10", "150.09", "300.5", "300.77"

表格结构如下:


# Campo   Tipo    Collation   Attributi   Null

    1   id  int(20)         No  Nessuno AUTO_INCREMENT   
    2   giorno  date            Sì  NULL    
    3   lmedollton  float           Sì  NULL         
    4   changedolleuro  float           Sì  NULL         
    5   euroton float           Sì  NULL         
    6   lmesterton  float           Sì  NULL         
    7   delnotiz    float           Sì  NULL          
    8   girm    float           Sì  NULL         
    9   sgm float           Sì  NULL

有两个问题:
1) 使用以下php代码:

$csvFile = "../scripts/tabella.csv";

        $db = @mysql_connect('***', '***', '***');
        @mysql_select_db('***');

        $query = 'LOAD DATA LOCAL INFILE \' '. $csvFile .' \' INTO TABLE rame FIELDS TERMINATED BY \',\' ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' 
(giorno,lmedollton,changedolleuro,euroton,lmesterton,delnotiz,girm,sgm)';

        if(!mysql_query($query)){
            die(mysql_error());
        }
        //@mysql_query($query);

        mysql_close($db);

mysqlèerror返回'找不到文件';我也尝试过使用realpath函数,但是错误是一样的。csv文件位于中的站点服务器中www.valuebiz.it/valuebiz/scripts/tabella.csv 其权限为777。像url一样使用此路径,我可以正确下载tabella.csv。我应该用哪条路?
2) 我试图在localhost中运行查询,但添加的记录的每个字段都是空的。该表有9个字段,包括id字段(自动递增):第一个是日期类型,其他是浮点类型。为什么插入值为空?
你能帮帮我吗?谢谢!

tcbh2hod

tcbh2hod1#

我认为你们的进口尝试有三个主要问题。首先,如果文件路径错误,则不会移动任何内容。第二,你应该使用 IGNORE 1 LINES 在命令的末尾告诉mysql,您的csv文件在开头有一个单行头。第三,我认为你应该使用 LOAD DATA INFILE ,不是 LOAD DATA LOCAL INFILE ,因为您是从服务器而不是客户端执行此代码。试着按照这些思路来做:
Windows:

LOAD DATA INFILE 'C:/path/to/data.csv' INTO TABLE rame
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(giorno, lmedollton, changedolleuro, euroton, lmesterton, delnotiz, girm, sgm);

unix系统:

LOAD DATA INFILE '\path\to\data.csv' INTO TABLE rame
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(giorno, lmedollton, changedolleuro, euroton, lmesterton, delnotiz, girm, sgm);

相关问题