【问题标题】:Python snowflake connection error because of spaces in query由于查询中的空格,Python雪花连接错误
【发布时间】:2022-01-06 03:35:22
【问题描述】:

我正在尝试在我的 python 笔记本中运行 sql 查询,

单元格中的代码看起来像

sql = "select date, count(distinct id)
from table
group by 1;"

当我运行时,我得到一个错误

SyntaxError: unterminated string literal (detected at line 1)

我想我发现了错误,当我删除行之间的空格时,它会消失并运行,例如:

"select date, count(distinct id) from table group by 1;"

问题是我的示例查询,但是我有大型查询,很难将它们退格到一行中,有没有一种方法可以让我在没有一行的情况下运行它?谢谢

【问题讨论】:

    标签: python sql python-3.x


    【解决方案1】:

    你可以三引号而不是双引号:“”“string”””或“'string”' 这将允许您在单独的行而不是一行上进行查询。例如,如果你有一个大查询,你可以让它多行

    sql = """SELECT *
             FROM TABLE
             WHERE 1=1
             AND stuff = stuff"""
    

    【讨论】:

      【解决方案2】:

      在 python 中,您可以使用三引号 """ 创建多行字符串。 例如,这将引发 SyntaxError:

      string = "hello
      there"
      

      但这不会:

      string = """hello
      there"""
      

      【讨论】:

        猜你喜欢
        • 2020-03-23
        • 2021-11-11
        • 1970-01-01
        • 2020-11-05
        • 2022-01-24
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多