“1064 - 你的 SQL 语法有错误”当使用字段规范加载数据 INFILE 时
"1064 - You have an error in your SQL syntax" when LOAD DATA INFILE with fields specification
我花了几个小时阅读了本网站上可能超过 10 个不同的问答,并多次查看了我的以下查询,但仍然找不到问题所在。这是我的 PHP
代码中的查询定义行:
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
$filename
是在上传CSV
文件后设置的。非常相似的代码在 phpMyAdmin
中工作,但我无法在我的 PHP
代码中工作。我不断收到此错误:
1064 - 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 '' IGNORE 1 LINES (field_1
, field_2
, field_3
, field_4
, field_5
, field_6
, ' at line 2
我怀疑它与 LINES TERMINATED BY '\r\n'
有关,但我没有发现它有任何问题,因为完全相同的行终止定义在代码的其他部分工作。
感谢您的帮助。
简单测试,只需编写一个小脚本,然后 运行 在 CLI 中查看从字符串输出的内容。
$filename = 'dsafsdf';
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\\"' ESCAPED BY '\\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
echo $q;
输出为
LOAD DATA INFILE 'dsafsdf'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\'
LINES TERMINATED BY '
' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)
请注意缺少的 \
,因为您位于双引号字符串中,所以已将其解释为 excape 字符。所以你现在需要做的就是添加相关的转义次数以获得像这样的正确输出
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\\"' ESCAPED BY '\\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
echo $q;
生成预期的结果
LOAD DATA INFILE 'dsafsdf'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)
我花了几个小时阅读了本网站上可能超过 10 个不同的问答,并多次查看了我的以下查询,但仍然找不到问题所在。这是我的 PHP
代码中的查询定义行:
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
$filename
是在上传CSV
文件后设置的。非常相似的代码在 phpMyAdmin
中工作,但我无法在我的 PHP
代码中工作。我不断收到此错误:
1064 - 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 '' IGNORE 1 LINES (
field_1
,field_2
,field_3
,field_4
,field_5
,field_6
, ' at line 2
我怀疑它与 LINES TERMINATED BY '\r\n'
有关,但我没有发现它有任何问题,因为完全相同的行终止定义在代码的其他部分工作。
感谢您的帮助。
简单测试,只需编写一个小脚本,然后 运行 在 CLI 中查看从字符串输出的内容。
$filename = 'dsafsdf';
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\\"' ESCAPED BY '\\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
echo $q;
输出为
LOAD DATA INFILE 'dsafsdf'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\'
LINES TERMINATED BY '
' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)
请注意缺少的 \
,因为您位于双引号字符串中,所以已将其解释为 excape 字符。所以你现在需要做的就是添加相关的转义次数以获得像这样的正确输出
$q="LOAD DATA INFILE '$filename'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\\"' ESCAPED BY '\\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)";
echo $q;
生成预期的结果
LOAD DATA INFILE 'dsafsdf'
IGNORE INTO TABLE `temp_data_1`
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\'
LINES TERMINATED BY '\r\n' IGNORE 1
LINES (`field_1`, `field_2`, `field_3`, `field_4`, `field_5`,
`field_6`, `field_8`, `field_9`)
SET `field_7` = SUBSTRING_INDEX(`field_6`, '.',-1)