繁体   English   中英

mysql php上的语法错误加载数据infile

[英]Syntax Error on mysql php load data infile

这是我第一次尝试将csv文件中的数据导入mysql表。

问题是csv列的顺序与表中的顺序不匹配,并且csv中缺少一列(自动增量) id 我正在尝试这样做,但是它给了我语法错误。

        $sql="LOAD DATA INFILE '$file' 
 INTO TABLE calls 
 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' 
 OPTIONALLY ENCLOSED BY ".'"'." 
 (`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";

注意$file是动态生成的文件名。 执行查询时出现此错误。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTIONALLY ENCLOSED BY " (`account_code`,`source`,`destination`,`dbcontext`,`' at line 4

知道为什么吗?

谢谢阿马尔

这个:

OPTIONALLY ENCLOSED BY "

需要是

OPTIONALLY ENCLOSED BY '"'

所以你的代码应该改为

$sql="LOAD DATA INFILE '$file' 
 INTO TABLE calls 
 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
 LINES TERMINATED BY '\r\n' 

 (`account_code`,`source`,`destination`,`dbcontext`,`caller_id`,`channel`,`destination-channel`,`lastapp`,`lastdata`,`start`,`answer`,`end`,`duration`,`billseconds`,`disposition`,`amaflag`,`call_id`,`userfield`)";

(更改所需的单引号和交换子句的顺序已更改,但我还将您的连接更改为更具可读性的内容)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM