How do I use SQL parameters with python?

After creating a connection object db:

cursor = db.execute('SELECT * FROM Customer WHERE CustomerID = %s', [customer_id])

then use any of the fetch... methods of the resulting cursor object.

Don’t be fooled by the %s part: this is NOT string formatting, it’s parameter substitution (different DB API modules use different syntax for parameter substitution — pymssql just happens to use the unfortunate %s!-).

Leave a Comment