Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MySQLdb TypeError: not all arguments converted during string formatting

Upon running this script:

#! /usr/bin/env python import MySQLdb as mdb import sys      class Test:     def check(self, search):         try:             con = mdb.connect('localhost', 'root', 'password', 'recordsdb');              cur = con.cursor()             cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search )              ver = cur.fetchone()              print "Output : %s " % ver          except mdb.Error, e:              print "Error %d: %s" % (e.args[0],e.args[1])             sys.exit(1)          finally:                 if con:                     con.close()  test = Test() test.check("test") 

I get an error of:

./lookup  Traceback (most recent call last):   File "./lookup", line 27, in <module>     test.check("test")   File "./lookup", line 11, in creep     cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search )   File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 187, in execute     query = query % tuple([db.literal(item) for item in args]) TypeError: not all arguments converted during string formatting 

I have zero idea why. I'm trying to do parameterized querys, but it's been nothing but a pain. I'm somewhat new to Python, so it's probably an obvious problem.

like image 593
Matthew 'mandatory' Bryant Avatar asked Feb 12 '14 21:02

Matthew 'mandatory' Bryant


People also ask

How do I fix TypeError is not all arguments converted during string formatting in Python?

Such a common error is TypeError: not all arguments converted during string formatting. This error is caused when there is a mismatch in data types and strings are not properly formatted. The solution to this error is to use proper string formatting functions such as int() or str() to obtain the desired data type.

How to fix not all arguments converted during string formatting?

The Python "TypeError: not all arguments converted during string formatting" occurs when we use incorrect syntax to format a string or use the % operator with a string and a number. To solve the error, call the format() method on the string and provide values for all placeholders.

What does this mean TypeError not all arguments converted during string formatting?

Resolving "typeerror: not all arguments converted ... in Python" In Python, “typeerror: not all arguments converted during string formatting” primarily occurs when: There is no format specifier. The format specifiers and the number of values are different.


1 Answers

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] ) 

See the MySQLdb documentation. 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).

like image 127
kevinsa5 Avatar answered Oct 01 '22 13:10

kevinsa5