How to format long SQL queries according to PEP8

In this way you have a clearly looking SQL statement that complies with PEP8 guidelines.

cursor.execute("""
    SELECT pivot_id
    FROM aud_qty
    WHERE hshake1 is NULL
        AND ((strftime('%s', DATETIME('now')) -
            strftime('%s', sent_to_pivot)) / (60)) > 30;
""")

You need to use multiline strings. If you declare your string with just one " or ', it will be a single line string, to create multiline strings, you need to wrap your string with """ or '''. Here is an example:

sql_query = """SELECT pivot_id 
FROM aud_qty 
WHERE hshake1 is NULL AND 
    ((strftime('%s', DATETIME('now')) - strftime('%s', sent_to_pivot)) / (60)) > 30;"""

It's worth mentioning that creating SQL queries manually is usually not a good idea, as it can enable SQL injection attacks and lead to other problems.


What about

cursor.execute("""SELECT pivot_id
                    FROM aud_qty
                   WHERE hshake1 is NULL
                     AND ((strftime('%s', DATETIME('now')) -
                         strftime('%s', sent_to_pivot)) / (60)) > 30;
               """)

? Using """ or ''' you get the same behaviour as a very long string but you can use newlines just fine. And your database won't mind them either.