1

I am trying to select an array of values from my MySQL without using a for loop. The for loop takes to long and I want to grab all of the values at once. I do not know what is wrong with my arguments and I am having a hard time interpreting what the error message I receive means.

zipcode = ["37204", "60964", "60068"]
connection = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database", cursorclass=MySQLdb.cursors.SSCursor)
cursor = connection.cursor()  
query = "SELECT fips FROM us WHERE zip = %s"
cursor.executemany(query,zipcode)
results = cursor.fetchall()

The error looks like this:

query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting

Any help is appreciated.

1
  • In the declaration of the zipcode list, your comma is inside the first element. Is that a typo in the question or in your code? Commented Jun 3, 2014 at 19:27

1 Answer 1

1

Python is not my forte and I've not used executemany before, but I don't think it's supposed to be used for executing code that's supposed to return something. You probably want to use IN with your query.

query = "SELECT fips FROM us WHERE zip IN ('%s')" % "','".join(zipcode)
cursor.execute(query)
results = cursor.fetchall()
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think this is safe, but if you have your inputs controlled somewhere else this should work

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.