1

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

2 Answers 2

4

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"""
Sign up to request clarification or add additional context in comments.

Comments

2

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"""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.