1

When i execute below program

import MySQLdb
cn = MySQLdb.connect(host="localhost", user="root", passwd="mysqlroot", 
db="sv_data")
cursor = cn.cursor()
cursor.execute("select addressline1, zipcode from sv_address where zipcode = '10011'")
for (addressline1, zipcode) in cursor:
print(addressline1, zipcode)
cursor.close()
cn.close()

it works fine. However when i try to add a parameter in query, like below

import MySQLdb
 cn = MySQLdb.connect(host="localhost", user="root", passwd="****",     
db="sv_data")
cursor = cn.cursor()

a="10011"
cursor.execute("select addressline1, zipcode from sv_address where zipcode = 
%s", (a))
for (addressline1, zipcode) in cursor:
 print(addressline1, zipcode)

cursor.close()
cn.close()

getting error ProgrammingError: not all arguments converted during string formatting

can you please advise how to fix this. I tried various options. zipcode is varchar field in mysqldb

3
  • did you try - select addressline1, zipcode from sv_address where zipcode = '%s' as query ? Commented Aug 12, 2017 at 6:00
  • what is the traceback you are getting? Commented Aug 12, 2017 at 6:07
  • in <module> cursor.execute("select addressline1, zipcode from sv_address where zipcode = '%s'", (a)) File "C:\ProgramData\Anaconda3\lib\site-packages\MySQLdb\cursors.py", line 240, in execute self.errorhandler(self, ProgrammingError, str(m)) File "C:\ProgramData\Anaconda3\lib\site-packages\MySQLdb\connections.py", line 52, in defaulterrorhandler raise errorclass(errorvalue) ProgrammingError: not all arguments converted during string formatting Commented Aug 12, 2017 at 6:14

2 Answers 2

2

You are not passing the argument. you are rather giving it a string.

Try this.

cursor.execute("select addressline1, zipcode from sv_address where zipcode = %s" % a )

The best way to do is to separate your arguments and sql_query

_sql = "select addressline1, zipcode from sv_address where zipcode = {0}"
cursor.execute(_sql.format(a))
Sign up to request clarification or add additional context in comments.

9 Comments

The docs show that this is valid: dev.mysql.com/doc/connector-python/en/…
Thanks. This works. However having a % is security issue. can you please help with passing as argument also
This shouldn't be an accepted answer. This is horribly unsafe.
@Adisheshaiah what if you use a dict argument like shown in the docs? Does it say the same thing?
I am using the second part. by separating arguments and query, and that works fine
|
1

I believe the issue is that cursor is reading (a) as just a string and sees multiple values in it that aren't being converted(1, 0, 0, 1, 1). So if you add a comma like so:

cursor.execute("select addressline1, zipcode from sv_address where zipcode = %s", (a,))

I believe that will work properly.

For instance, look at this function and the outcome:

In [28]: def t(arg):
    ...:     print(type(arg))
    ...:     

In [29]: t(('a'))
<class 'str'>

The argument isn't being read as a tuple, it's being read as a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.