使用Python的三引号字符串,我可以定义包含'
、反引号或"
的字符串,而不必关心它们:
hello = """
This string is bounded by triple double quotes (3 times ").
Unescaped newlines in the string are retained, though
it is still possible to use all normal escape sequences.
Whitespace at the beginning of a line is
significant.
This string ends in a newline.
"""
在PHP中,我可以使用heredoc (<<<
):
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
我如何在MySQL中做同样的事情?现在我必须确保转义我用来分隔的字符,例如,如果我使用'
作为分隔符,我必须在字符串中使用\'
:
UPDATE article
SET article_text ='
Extra-virgin olive oil is a staple in any chef\'s kitchen.
'
我希望能够像这样
UPDATE article
SET article_text ='''
Extra-virgin olive oil is a staple in any chef's kitchen.
'''
发布于 2014-05-28 04:42:44
您还可以在一行中使用两个单引号(''
),而不是对单引号(\'
)进行反斜杠转义。
据我所知,除了双单引号之外,没有更简单的方法了。没有等同于"""
的东西。这是可悲的。
但是,正如前面提到的,您最好的选择(如果您想节省时间)是根本不使用任何基于here的解决方案;最常见的情况是,数据库由用另一种语言编写的程序访问,或者由用另一种语言编写的程序生成的SQL脚本访问。
我还没有遇到过不支持prepared statements的编程语言,而且为了方便起见,它至少有一些SQL转义函数。
https://stackoverflow.com/questions/23898130
复制相似问题