gpt4 book ai didi

python - MySQL 相当于 Python 的三重引号字符串?

转载 作者:行者123 更新时间:2023-11-29 01:57:40 33 4
gpt4 key购买 nike

使用 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.
'''

最佳答案

您还可以在一行中使用两个单引号 ('') 而不是反斜杠转义单引号 (\')。

据我所知,除了双引号外,没有更简单的方法。没有等同于 """ 的东西。这很可悲。

但确实是您最好的选择(如果您想节省时间),如前所述here根本不使用任何基于 SQL 的解决方案;最常见的数据库是由用另一种语言编写的程序访问的,或者是由用另一种语言编写的程序生成的 SQL 脚本访问的。

我还没有遇到过不支持 prepared statements 的编程语言并且至少有一些 SQL 转义函数以方便使用。

关于python - MySQL 相当于 Python 的三重引号字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23898130/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com