jdoe jdoe - 1 year ago 165
Python Question

Python3.4 int object is not iterable - MySQLdb

I'm running python3.4 and i'm trying to run a query with the MySQLdb library.

I've done one successful query but now I am stuck when it comes to integers in a query. Here's the code:

location = player_info[6]

query2 = ("SELECT name FROM locations WHERE id=%d");
execute = cursor.execute(query2, (location));

Value of the variable location is 2. I just keep getting the error:

File "login.py", line 75, in tryLogin
execute = cursor.execute(query2, (location));

File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 195, in
args = tuple(map(db.literal, args)) TypeError: 'int' object is not iterable

I tried to change the %d to %s because someone said it should all be in string format but I still get the same error.

I have also tried changing "location" variable to simply:

location = 2

Any ideas?


All I had to do was to use %s and add a comma ( , ) after "location", so that the last line looks like this:

execute = cursor.execute(query2, (location, ));

Answer Source
            execute = cursor.execute(query2, (location));

In python, (location) is a parenthesized expression, not a tuple. In order to force that to be a tuple, you need to add a comma: (location,)

            execute = cursor.execute(query2, (location,));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download