ODBC Driver for SQL Server

Build 21.0.7958

Parameterized Statements

The following code example shows how to bind parameters to create parameterized statements.

Single-Use Statements

The execute method accepts an additional sequence for binding query parameters to values.

cursor.execute("SELECT Id, ProductName FROM \"SampleDB\".\"Sample\".Products WHERE ProductName = ?", ("Konbu",))
for (Id, ProductName) in cursor:
    print("Id = {}, ProductName = {}".format(Id, ProductName))

Multi-Use Statements

The executemany method can be used to execute the same query repeatedly with different sets of parameters. Instead of a sequence of parameters, executemany accepts a nested sequence of parameters which are used for each execution.

executemany works only with INSERT, UPDATE and DELETE statements. It cannot be used with any operation that returns results, such as a SELECT or an EXECUTE.

params = [
  ("Konbu 1",),
  ("Konbu 2",),
  ("Konbu 3",),

cursor.executemany("INSERT INTO \"SampleDB\".\"Sample\".Products(ProductName) VALUES (?)", params)

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 21.0.7958