semi-concatenated strings

Skip Montanaro skip at pobox.com
Fri May 31 12:10:23 EDT 2002


    Grant>     rows = self.executesql("select cities.city, state, country"          +
    Grant>                            "    from cities, venues, events, addresses"  +
    Grant>                            "    where     cities.city like %s"           +
    Grant>                            "          and events.active = 1"             +
    Grant>                            "          and venues.address = addresses.id" +
    Grant>                            "          and addresses.city = cities.id"    +
    Grant>                            "          and events.venue = venues.id",
    Grant>                            (city,))

    Grant> which doesn't look so bad.  (But then again, without the
    Grant> backslash it's less explicit <wink>.)

You can avoid the backslash with a pair of parens.  Recasting my original
example a little:

    rows = self.executesql(("select cities.city, state, country"       +
                            "  from cities, venues, events, addresses" +
                            "  where cities.city like %s"              +
                            "    and events.active = 1"                +
                            "    and venues.address = addresses.id"    +
                            "    and addresses.city = cities.id"       +
                            "    and events.venue = venues.id"),
                           (city,))

I'm surprised nobody commented on the rather weird indentation of the SQL
statement in my original example.  I use the style in this last example now
(well, without the extra parens and the plus signs).

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)
Boycott Netflix - they spam - http://www.musi-cal.com/~skip/netflix.html






More information about the Python-list mailing list