How to return multiline sql queries in Python

46 Views Asked by At

I want to run a sql query in a server. And the query is too long, so I am not able to figure out how it should be broken down:

The query looks something like this:

return("usr/<path> -e 'select val1 from table_name where val2 = (select val3 f rom another_table where val = 'abc')'")

Also, I can't post the exact query as its related to my work. So now the issue is, if I run a select query which comes in a single line of the return statement, then the value gets returned successfully. But in case of this long query, I am getting Syntax errors as it gets psuhed to the next line. Also, the code has to be compatible with both Python version 2 and 3. Can someone please help?

1

There are 1 best solutions below

0
Talha Tayyab On BEST ANSWER

You need to use triple quotes:

Something like:

return("""usr/<path> -e 'select val1 from table_name 
where val2 = (select val3 f rom another_table where val = 'abc')'""")

Triple quoted strings maintain newlines.