[Tutor] How to break long lines?

Jerry Hill malaclypse2 at gmail.com
Fri Feb 22 22:59:00 CET 2013


On Fri, Feb 22, 2013 at 4:26 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
> I am cleaning up my code and have a number of sqlite3 execute statements
> that extend far past 80 characters.
>
> From my reading implicit line joining with (), [] or {} seems to be the
> preferred method, but
>
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>         COLLATE NOCASE', cat)
>
> gives this error:
>
> jfb at jims1204:~/MyProgs/passwords$ python passwords.py
>   File "passwords.py", line 50
>     cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>                                                                           ^
> SyntaxError: EOL while scanning string literal

Single quoted strings aren't allowed to have line breaks in them.  If
you have two string literals separated only by whitespace, though,
they get joined together, so you could do this:

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account'
        'COLLATE NOCASE', cat)

You can also use triple quoted strings instead, which is my
preference.  Triple quoted strings are allowed to have line breaks,
and the whitespace doesn't matter in your SQL query.  So I'd do
something like this:

cur.execute ('''select account
                from pwds
                where category = ?
                order by account
                collate nocase''', cat)

You can break the query up over however many lines it needs to be
readable, of course.

-- 
Jerry


More information about the Tutor mailing list