close

[Solved] Python MySQLdb Type: not all arguments converted during string formatting

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python MySQLdb Type: not all arguments converted during string formatting in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python MySQLdb Type: not all arguments converted during string formatting Error Occurs?

Today I get the following error Python MySQLdb Type: not all arguments converted during string formatting in python.

How To Solve Python MySQLdb Type: not all arguments converted during string formatting Error ?

  1. How To Solve Python MySQLdb Type: not all arguments converted during string formatting Error ?

    To Solve Python MySQLdb Type: not all arguments converted during string formatting Error he reasoning is that execute's second parameter represents a list of the objects to be converted, because you could have an arbitrary number of objects in a parameterized query.

  2. Python MySQLdb Type: not all arguments converted during string formatting

    To Solve Python MySQLdb Type: not all arguments converted during string formatting Error he reasoning is that execute's second parameter represents a list of the objects to be converted, because you could have an arbitrary number of objects in a parameterized query.

Solution 1

Instead of this:

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

Try this:

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

The reasoning is that execute‘s second parameter represents a list of the objects to be converted, because you could have an arbitrary number of objects in a parameterized query. In this case, you have only one, but it still needs to be an iterable (a tuple instead of a list would also be fine).

Solution 2

‘%’ keyword is so dangerous because it major cause of ‘SQL INJECTION ATTACK’.
So you just using this code.

cursor.execute("select * from table where example=%s", (example,))

or

t = (example,)
cursor.execute("select * from table where example=%s", t)

if you want to try insert into table, try this.

name = 'ksg'
age = 19
sex = 'male'
t  = (name, age, sex)
cursor.execute("insert into table values(%s,%d,%s)", t)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read