DB-API execute params, am I missing something?

Paul Boddie paul at boddie.org.uk
Wed May 27 05:45:18 EDT 2009


On 26 Mai, 13:46, Gabriel Rossetti <gabriel.rosse... at arimaz.com>
wrote:
>
> def getParams(curs):
>     curs.execute("select * from param where id=%d", 1001)

First of all, you should use the database module's parameter style,
which is probably "%s" - something I've thought should be deprecated
for a long time due to the confusion with string substitution that
this causes, regardless of whether that mechanism is actually used
internally by the module (as seen in your traceback).

You also have to provide a sequence of parameters: unlike string
substitution, you cannot provide a single parameter value and expect
the execute method to do the right thing. Some database modules seem
to insist on either lists or tuples of parameters, but I think any
sequence type should work.

Try this:

  curs.execute("select * from param where id=%s", (1001,))

Paul



More information about the Python-list mailing list