Home >Backend Development >Python Tutorial >Why Does My Parameterized MySQL Query Throw a 'TypeError: not all arguments converted during string formatting'?
Parameterized SQL Queries: Navigating "TypeError: not all arguments converted during string formatting"
In MySQLdb, executing parameterized SQL queries can lead to a "TypeError: not all arguments converted during string formatting." This error occurs when the query expects a specific number of arguments, but the arguments provided do not match the expectation.
To rectify this error, replace the legacy syntax with a more modern approach. Instead of using:
cur.execute("SELECT * FROM records WHERE email LIKE '%s'", search)
Use this syntax:
cur.execute("SELECT * FROM records WHERE email LIKE %s", [search])
The second parameter in execute() is now a list containing the arguments to be converted. Each argument in this list must be convertible to the expected data type in the query. In this case, the query expects a string, so search needs to be enclosed in a one-element list.
The above is the detailed content of Why Does My Parameterized MySQL Query Throw a 'TypeError: not all arguments converted during string formatting'?. For more information, please follow other related articles on the PHP Chinese website!