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 Sid, Name FROM Applications WHERE Sid = ?", ("AP5ddf534702934bd3a446d293e8cdeb1f",)) for (Sid, Name) in cursor: print("Sid = {}, Name = {}".format(Sid, Name))
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 = [ ("MyApp2 1",), ("MyApp2 2",), ("MyApp2 3",), ] cursor.executemany("INSERT INTO Applications (Name) VALUES (?)", params)