Home > Backend Development > Python Tutorial > Why Does My Parameterized MySQL Query Throw a 'TypeError: not all arguments converted during string formatting'?

Why Does My Parameterized MySQL Query Throw a 'TypeError: not all arguments converted during string formatting'?

Linda Hamilton
Release: 2024-12-03 04:58:09
Original
244 people have browsed it

Why Does My Parameterized MySQL Query Throw a

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)
Copy after login

Use this syntax:

cur.execute("SELECT * FROM records WHERE email LIKE %s", [search])
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template