在SQL语法中有一个错误。

时间:2022-01-12 23:01:16

Was wondering if someone could find the error in this:

想知道是否有人会发现这个错误:

    $sqlinsert = "LOAD DATA LOCAL INFILE 
\'$target_path\' INTO TABLE 'db_usergroup' 
FIELDS TERMINATED BY \',\' ENCLOSED BY \'\"\' ESCAPED BY \'\\\\\' 
LINES TERMINATED BY \'\\r\\n\'";

$target_path varies slightly in its exact name, but an example would be:

$target_path的确切名称略有不同,但示例如下:

temp/24_09_12_16_57_27_invoice_example.csv

I get this error:

我得到这个错误:

    An error occurred. The file could not be imported. 
Error with MySQL Query: 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 
'\'temp/24_09_12_16_57_27_invoice_example.csv' INTO TABLE 
'db_usergroup' FIELDS T' at line 1

All help is greatly appreciated.

非常感谢大家的帮助。

2 个解决方案

#1


0  

Why are you escaping the quotes in your target path? I don't believe that's necessary.

你为什么要逃离目标路径的引号?我不认为这是必要的。

You can usually tell exactly what the error is by examining the first few characters after for the right syntax to use near, and this does seem to indicate the backslash is at fault.

通常,您可以通过检查前面几个字符来准确地判断错误,而这似乎表明反斜杠是错误的。

Try:

试一试:

$sqlinsert = "LOAD DATA LOCAL INFILE '$target_path' INTO TABLE ...

#2


0  

'db_usergroup' is invalid. The ' single quotes turn that tablename into a string, which MySQL will barf on. You do not need to quote/escape table or field names unless they are reserved words. And for that, you use backticks, e.g.

“db_usergroup”是无效的。单引号将tablename转换成一个字符串,MySQL将在该字符串中使用该字符串。您不需要引用/转义表或字段名,除非它们是保留字。因此,你可以使用backticks。

LOAD DATA ... `db_usergroup` ...
              ^--          ^--

followup:

跟踪:

$sqlinsert = <<<EOL
LOAD DATA LOCAL INFILE '$target_path'
INTO TABLE db_usergroup
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'
EOL;

#1


0  

Why are you escaping the quotes in your target path? I don't believe that's necessary.

你为什么要逃离目标路径的引号?我不认为这是必要的。

You can usually tell exactly what the error is by examining the first few characters after for the right syntax to use near, and this does seem to indicate the backslash is at fault.

通常,您可以通过检查前面几个字符来准确地判断错误,而这似乎表明反斜杠是错误的。

Try:

试一试:

$sqlinsert = "LOAD DATA LOCAL INFILE '$target_path' INTO TABLE ...

#2


0  

'db_usergroup' is invalid. The ' single quotes turn that tablename into a string, which MySQL will barf on. You do not need to quote/escape table or field names unless they are reserved words. And for that, you use backticks, e.g.

“db_usergroup”是无效的。单引号将tablename转换成一个字符串,MySQL将在该字符串中使用该字符串。您不需要引用/转义表或字段名,除非它们是保留字。因此,你可以使用backticks。

LOAD DATA ... `db_usergroup` ...
              ^--          ^--

followup:

跟踪:

$sqlinsert = <<<EOL
LOAD DATA LOCAL INFILE '$target_path'
INTO TABLE db_usergroup
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '\\' 
LINES TERMINATED BY '\r\n'
EOL;