semi-concatenated strings
Delaney, Timothy
tdelaney at avaya.com
Thu May 30 19:28:19 EDT 2002
> From: Skip Montanaro [mailto:skip at pobox.com]
>
> long strings. For example, I have code like this that
> queries a database:
>
> 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,))
[lines undented somewhat to prevent wrapping ...]
Just wondering Skip ... why would you write this rather than a triple-quoted
string?
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,))
which to me looks much cleaner and is much easier to modify.
Of course, I also would have bound the SQL to a name first ...
sql = """
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
"""
rows = self.executesql(sql.strip(), (city,))
I've yet to find a place where I've had a use for concatenating string
literals.
Tim Delaney
More information about the Python-list
mailing list