semi-concatenated strings

Skip Montanaro skip at pobox.com
Fri May 31 15:43:46 EDT 2002


    >> 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,))

    Grant> I dunno...I don't know anything about SQL, but your code looks
    Grant> sortta Pythonic.

Yeah it does.  It serves two purposes.  One it makes it clear to me where
the various clauses of the select statement begin ("from ...", "where ...").
It also guarantees I have at least one space between the different
fragments.  This:

    ("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")

might appear correct, but there are no spaces between the end of one
substring and the start of the next...

-- 
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