Syntax:
cursor.execute(operation, params=None)iterator = cursor.execute(operation, params=None)# Allowed before 9.2.0iterator = cursor.execute(operation, params=None, multi=True) This method executes the given databaseoperation (query or command). The parameters found in the tuple or dictionaryparams are bound to the variables in the operation. Specify variables using%s or%( parameter style (that is, usingname)sformat orpyformat style).
Before Connector/Python 9.2.0,execute() accepted amulti option and returned an iterator if set toTrue. That option was removed in 9.2.0, andSection 9.3, “Executing Multiple Statements” was added.
In Python, a tuple containing a single value must include a comma. For example,('abc') is evaluated as a scalar while('abc',) is evaluated as a tuple.
This example inserts information about a new employee, then selects the data for that person. The statements are executed as separateexecute() operations:
insert_stmt = ( "INSERT INTO employees (emp_no, first_name, last_name, hire_date) " "VALUES (%s, %s, %s, %s)")data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))cursor.execute(insert_stmt, data)select_stmt = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"cursor.execute(select_stmt, { 'emp_no': 2 }) The data values are converted as necessary from Python objects to something MySQL understands. In the preceding example, thedatetime.date() instance is converted to'2012-03-23'.
If the connection is configured to fetch warnings, warnings generated by the operation are available through theMySQLCursor.fetchwarnings() method.