[DB-SIG] pyformat Parameter Style
chris at cogdon.org
Sat May 10 12:05:40 EDT 2003
On Saturday, May 10, 2003, at 10:54 US/Pacific, M.-A. Lemburg wrote:
> The DB API says "Python extended format codes" meaning that all
> valid Python formatting codes (including their parameters) may
> be used.
That's very interesting. Looking at some of the code I have, using, for
example %(key)d would not work, even if the parameter was an integer,
because the API turns each and every single parameter into a string
regardless of the type.
This code snippet from pyPgSQL.PgSQL:
if len(parms) == 1 and \
(type(parms) in [DictType, ListType, TupleType]
parms = _quoteall(parms)
parms = tuple(map(_quote, parms));
self.res = self.conn.conn.query(_qstr % parms)
Notice how the parameters are 'quoted' first (which will turn them ALL
into strings), and THEN it's passed to the '%' operator. If you have
any %d in there, you'll get a TypeError.
And, take this docstring from pyPgSQL:
cursor.execute(query[, param1[, param2, ..., paramN])
Execute a query, binding the parameters if they are passed.
binding syntax is the same as the '%' operator except that
[or %(name)s] should be used and the %s [or %(name)s]
should not be
quoted. Any necessary quoting will be performed by the
("`-/")_.-'"``-._ Chris Cogdon <chris at cogdon.org>
. . `; -._ )-;-,_`)
(v_,)' _ )`-.\ ``-'
_.- _..-_/ / ((.'
((,.-' ((,/ fL
More information about the DB-SIG