mysql - 无法使用 load data infile 识别错误原因

标签 mysql sql

我正在将数据加载到存储过程内的表中,该存储过程的文件名作为参数传递。

 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

说实话,我迷路了,我不知道我在哪里搞砸了

最佳答案

\ 是转义序列的开头。

第一个问题很可能是缺乏转义单引号。因此,当您执行 \' 时,它会转换为单引号,从而终止未关闭的 block 。由于它现在已关闭,因此由于保留了字符串片段,您会收到语法错误。您需要执行 \\' 这会引入斜杠,而不是 foobar 之后的单引号。

有关转义序列表,请参阅 Mysql 手册页 String Literals .

即便如此,这整件事还是行不通。因为存储过程、事件等不支持LOAD DATA INFILE。这是一件令人悲伤的事情,但据称是出于安全原因。它将生成错误 42000 或类似错误。

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

解决方法是使用 UDF,这需要您修改 mysql 环境,或者为这些琐碎的事情创建一个外部进程,这就是我所做的。

关于mysql - 无法使用 load data infile 识别错误原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38241364/

相关文章:

MySQL:如何选择A列不在B列中的位置?

php - 字符被编码为 �

php或mysql以随机顺序从每个组合中选择数据

sql - 在presto SQL中将int格式化为日期

java - 是否可以使用SQL获取分区数据?

php - 无法连接php和mysql

javascript - jQuery AJAX 检查数据库中是否存在电子邮件由于某种原因无法工作

mysql - 查询上周添加的商品详情

sql - 使用从同一表计算的值更新表的最佳方法

sql - 如果 PostgreSQL 没有变化,事务是否记录到 WAL?