2016-07-07 69 views
0

我正在將數據加載到存儲過程中的一個表中,該存儲過程的文件名作爲參數傳遞。無法使用加載數據infile識別錯誤的原因

select @load_config:= concat('load data local infile ',config_file,' into table config fields terminated by \',\' lines terminated by \n'); 
prepare stm from @load_config; 

爲了這個,我正在一個錯誤

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '/mnt/appln/mysql/dvdbuser/config.csv into table config fields terminated by ',' ' at line 1 i tried this as well

select @load_config:= concat('load data local infile \'',config_file,'\' into table config fields terminated by \',\' lines terminated by \n'); 
prepare stm from @load_config; 

爲此,我得到了錯誤

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

說實話我失去了,我不知道在哪裏我搞砸了

回答

1

\是轉義序列的開始。

第一個問題很可能是沒有逃避單引號。所以當你做\'它翻譯成單引號,因此終止那個沒有關閉的塊。由於它現在被關閉,由於剩餘的字符串片段,您會收到語法錯誤。你需要做\\'這會帶來一個斜槓,而不是foobar之後的單引號。

請參閱Mysql手冊頁以獲取轉義序列表String Literals

即使如此,這整個事情將無法正常工作。因爲LOAD DATA INFILE在存儲過程,事件等中不受支持。這是一件可悲的事情,但據稱是出於安全原因。它會產生一個錯誤42000或類似的。

Error: This command is not supported in the prepared statement protocol yet

一種解決方法是使用UDF的需要你修改你的MySQL環境,或創造這麼小的事情外部的過程,是我做的。

+0

是的,我確實遇到過這個問題,我想通過文件作爲參數看起來像我現在無法做到這一點不管怎麼說不錯的答案 – avz2611

1

你應該使用SET將值分配給變量:

SET @load_config:= concat('load data local infile ',config_file,' into table config 
    fields terminated by \',\' lines terminated by \n'); 
+0

我完全錯過了'SET'部分的問題。看,我正在跟蹤你,以獎勵你在這個網站上的辛勤工作。 – Drew

相關問題