Differences between revisions 3 and 4
Revision 3 as of 2004-02-11 17:25:30
Size: 989
Editor: proxy10
Comment:
Revision 4 as of 2004-02-12 09:35:05
Size: 988
Editor: 193
Comment: Small syntactical correction
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
Don't, use the '%' concatenation operator, pass them as a series of extra parameters. For instance Don't use the '%' concatenation operator, pass them as a series of extra parameters. For instance

These are the frequently asked questions from the DB-SIG mailing list.

TableOfContents

How do I pass parameters to the cursor.execute method?

Don't use the '%' concatenation operator, pass them as a series of extra parameters. For instance

>>> cursor.execute("SELECT * FROM my_table WHERE my_column = '%s'" % "column_value") 

May do what you want, but more by accident than design. If you change it to;

>>> cursor.execute("SELECT * FROM my_table WHERE my_column = %s", "column_value") 

Then the DB-API module will make sure your value is correctly escaped and turned into an object appropriate for the database.

/!\ Drivers differ in the way the parameters are passed to .execute ()

  • a list: .execute ("... col = ?", ["value"])

  • variable arguments: .execute ("... col = ?", "value")

  • a dictionary: .execute ("... col = :arg", {'arg': "value"})

  • keyword args: .execute ("... col = :arg", arg = "value")

DbApiFaq (last edited 2010-03-09 04:12:39 by bb14f857)

Unable to edit the page? See the FrontPage for instructions.