简体   繁体   English

如何使用值中的引号从 bash 插入 mysql 表?

[英]How to insert into mysql table from bash with quotes in values?

I obtain the result of a bash file process into a variable.我将 bash 文件处理的结果转换为变量。 The variable content should be inserted into a mysql table from bash cmd:变量内容应从 bash cmd 插入到 mysql 表中:

content="File '/test.csv' was imported"

mysql --user=root --password=rootpw --database=$DB
    -e "INSERT INTO mytable VALUES(1, '$content')"

Result:结果:

ERROR 1064 (42000) at line 1: 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 '' was imported' at line 1

Question: how can I deal with single quotes contained in a variable, when inserting to mysql?问题:插入到 mysql 时,如何处理变量中包含的单引号?

I tried all variants of $content , '$content' and "$content" , but none of them worked.我尝试了$content'$content'"$content"的所有变体,但没有一个起作用。

Try escaping with an backslash.尝试使用反斜杠 escaping。

"SELECT This is Tom\'s Car."

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

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