[DB-SIG] API 3.0 limiting paramstyle to ['named', 'qmark'] is okay. ('format' is not desirable)

Daniele Varrazzo daniele.varrazzo at gmail.com
Sat May 18 02:07:53 CEST 2013


On Fri, May 17, 2013 at 5:22 PM, Michael Bayer <mike_mp at zzzcomputing.com> wrote:
>
> The major issue with format/pyformat is that too many DBAPIs take the easy way out and just run their string through Python's "%" operator.   This causes DBAPIs that use the format styles to behave differently from those that don't.   In particular, a problem I have with MySQL-python and *maybe* recent versions of psycopg2 (would have to check again) is that the behavior changes whether or not I pass parameters to cursor.execute(). The same statement that includes a "%" sign as part of the string must be passed without escaping when no list/dictionary is passed as the second argument, but when a list/dictionary is passed, the parsing changes and now the % sign has to be escaped normally.   This is an undocumented behavior that's directly a side effect of the DBAPI saying, "oh there's parameters, let's call "stmt % params", vs. not.

On this specific point you are right, and just yesterday I had to
apologise for the inconsistency with a colleague of mine. This is
something I would probably fix in an eventual "psycopg3" that would
break compatibility with the past, but note that it would only affect
functions that use the % operator and have no parameter: a minimal
number (can't remember on top of my head if the % is used for other
syntactical elements in postgres: not that I recall).

OTOH if you want consistent behaviour with the %s and %%s just pass an
empty tuple to queries taking no argument. You are a framework, you
can do that (I would do that in psycopg by specifying the empty tuple
as default argument for execute's params, or or something semantically
equivalent).

-- Daniele


More information about the DB-SIG mailing list