Home > database >  Python snowflake connection error because of spaces in query
Python snowflake connection error because of spaces in query

Time:01-06

I am trying to run a sql query in my python notebook,

code in cell looks like

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

When I run I get an error of

SyntaxError: unterminated string literal (detected at line 1)

I think I found the error, when I delete the space between the lines it goes away and runs such as:

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

The problem is thats my sample query, but I have large queries where it gets difficult to backspace them into one line, is there a way where I can get this to run without having on one line? Thanks

CodePudding user response:

You can triple quotes and not double: “”” string””” or ”’ string”’ This will allow you to have your query on seperate lines rather that one line. For example, if you have a large query, you can have it go on multiple lines

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

CodePudding user response:

In python, you can create multi line strings using triple quotes """. For example, this will raise a SyntaxError:

string = "hello
there"

but this won't:

string = """hello
there"""
  •  Tags:  
  • Related