Split a long SQL query inside a variable

pep8, python

Solution

Use a triple quoted string:

query= """\
select a,b,c,d,e,f,g,h from a_very_long_tablename
where a_very_long_sql_statement='is_really_very_long'
or a_very_long_sql_statement='is_really_really_very_long'
"""

SQL is a whitespace-agnostic language, you can use newlines as well as spaces to delimit. The initial backslash escapes the first newline; it's a personal preference but not needed.

If you really don't want the newlines, put parenthesis around your string; no need to use `+` signs then as python will make it one long string for you:

query = (
    "select a,b,c,d,e,f,g,h from a_very_long_tablename "
    "where a_very_long_sql_statement='is_really_very_long' "
    "or a_very_long_sql_statement='is_really_really_very_long'")

Problem

Following the PEP8 guidelines, what would be the best practice to format a very long sql wistatement into a variable? An example bellow, of how im splitting the variable: ``` var= "some value" query = "select a,b,c,d,e,f,g,h from a_very_long_tablename" +\ "where a_very_long_sql_statement='is_really_very_long' " +\ "or a_very_long_sql_statement='" + var + "' order by a" ```

Original source