8

I got the following error:

  File "/home/ec2-user/test/test_stats.py", line 43, in get_test_ids_for_id
    cursor.execute("""select test_id from test_logs where id = %s """, (id))
  File "/home/ec2-user/.etl/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: 'int' object is not iterable

Here's the section of my code I'm having trouble with:

def get_test_ids_for_id(prod_mysql_conn, id):
    cursor = prod_mysql_conn.cursor()
    cursor.execute("""select test_id from test_logs where id = %s """, (id))
    rows = cursor.fetchall()
    test_ids = []
    for row in rows:
      test_ids.append(row[0])
    return test_ids
4
  • 2
    That should be cursor.execute(..., (id,)) - note the trailing comma, which makes it a tuple. Commented Feb 6, 2015 at 12:14
  • @jonrsharpe Can you explain clearly? Commented Feb 6, 2015 at 12:16
  • Martijn already has! See e.g. stackoverflow.com/q/22460082/3001761 Commented Feb 6, 2015 at 12:18
  • 1
    hello try like this [id] Commented Feb 6, 2015 at 12:18

2 Answers 2

26

You need to give cursor.execute a tuple, but you only gave it one integer:

(id)

Add a comma to make that a tuple:

(id,)

The full line then'd be:

cursor.execute("""select test_id from test_logs where id = %s """, (id,))

Putting an expressione in parentheses just 'groups' that one expression. It is the comma that makes something a tuple:

>>> (42)
42
>>> (42,)
(42,)

Any iterable will do really, so you could also use [...] brackets:

cursor.execute("""select test_id from test_logs where id = %s """, [id])
Sign up to request clarification or add additional context in comments.

9 Comments

Very annoying dealing with this
@briankip: why is it annoying? It's basic syntax; programming languages have always used the same symbols for different meanings in different contexts. Commas are used in tuples and when separating different parameters to a function signature, or for separating base classes in a class definition, or for separating the arguments in a call expression, or the values in a list, etc. Because you sometimes need to distinguish between a tuple and the other meanings, you need to use parentheses around the tuple to disambiguate it. But it is still the comma that makes something a tuple.
@briankip: and this is not MySQLdb specific. It is common among all Python database adapters; they all follow the Python Database API Specification
@briankip: it works like a normal function with parameters. The second argument to the cursor.execute() function is the query parameters argument. It is a single object holding all values for the parameters. If you have just one parameter, then yes, that's a single-element tuple. For 2 or more, you get the 2 or more values in that one object. It can be a tuple or a list.
@briankip: if you use named parameters, then you use a mapping (dictionary), with parameter names as the keys.
|
0

Basically what is happening is when you are passing a argument in query it is not becoming an iterable because it is coming in bracket. In order to make it iterable you need to make it in that form like list or tuple. Soo you can either add comma at the end (1,) or make it a list [1].

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.