Reputation: 1998
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
Upvotes: 1
Views: 850
Reputation: 1104
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"""
Upvotes: 2
Reputation: 56
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"""
Upvotes: 4