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, Column1 FROM Test_xlsx_Sheet1 WHERE Column2 = ?", ("Bob",)) for (Id, Column1) in cursor: print("Id = {}, Column1 = {}".format(Id, Column1))
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 = [ ("John 1",), ("John 2",), ("John 3",), ] cursor.executemany("INSERT INTO Test_xlsx_Sheet1 (Column1) VALUES (?)", params)