Home > Backend Development > Python Tutorial > How to Fix 'TypeError: Not All Arguments Converted' in Parameterized MySQL Queries?

How to Fix 'TypeError: Not All Arguments Converted' in Parameterized MySQL Queries?

Linda Hamilton
Release: 2024-12-10 14:04:10
Original
348 people have browsed it

How to Fix

TypeError: Not All Arguments Converted During String Formatting in Parameterized SQL Queries

Your code for a parameterized SQL query throws the error "TypeError: not all arguments converted during string formatting" because you're trying to directly substitute a string into the query. This approach doesn't work because it expects a list of arguments to be converted into the query.

To rectify this issue, rather than using:

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

modify your code to:

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

As per the MySQLdb documentation, the second parameter of execute() represents a list of objects to be converted, allowing for an arbitrary number of objects in a single query. In your case, despite having only one object, it still needs to be an iterable.

The above is the detailed content of How to Fix 'TypeError: Not All Arguments Converted' in Parameterized MySQL Queries?. 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