TypeError: 'int' object does not support indexing

Question:

I have this query:

some_id = 1

cursor.execute('
    SELECT "Indicator"."indicator" 
    FROM "Indicator" 
    WHERE "Indicator"."some_id" =   %s;', some_id)

I get the following error:

TypeError: 'int' object does not support indexing

some_id is an int but I’d like to select indicators that have some_id = 1 (or whatever # I decide to put in the variable).

Asked By: nlr25

||

Answers:

You should pass query parameters to execute() as a tuple (an iterable, strictly speaking), (some_id,) instead of some_id:

cursor.execute('
    SELECT "Indicator"."indicator" 
    FROM "Indicator" 
    WHERE "Indicator"."some_id" =   %s;', (some_id,))
Answered By: alecxe
cursor.execute('
    SELECT "Indicator"."indicator" 
    FROM "Indicator" 
    WHERE "Indicator"."some_id" =   %s;', [some_id])

This turns the some_id parameter into a list, which is indexable. Assuming your method works like i think it does, this should work.

The error is happening because somewhere in that method, it is probably trying to iterate over that input, or index directly into it. Possibly like this: some_id[0]

By making it a list (or iterable), you allow it to index into the first element like that.

You could also make it into a tuple by doing this: (some_id,) which has the advantage of being immutable.

Answered By: Stephan

Your id needs to be some sort of iterable for mogrify to understand the input, here’s the relevant quote from the frequently asked questions documentation:

>>> cur.execute("INSERT INTO foo VALUES (%s)", "bar")    # WRONG
>>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar"))  # WRONG
>>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar",)) # correct
>>> cur.execute("INSERT INTO foo VALUES (%s)", ["bar"])  # correct

This should work:

some_id = 1

cursor.execute('
    SELECT "Indicator"."indicator" 
    FROM "Indicator" 
    WHERE "Indicator"."some_id" =   %s;', (some_id, ))
Answered By: pmcnamee

Slightly similar error when using Django:

TypeError: 'RelatedManager' object does not support indexing

This doesn’t work

mystery_obj[0].id

This works:

mystery_obj.all()[0].id

Basically, the error reads Some type xyz doesn't have an __ iter __ or __next__ or next function, so it's not next(), or itsnot[indexable], or iter(itsnot), in this case the arguments to cursor.execute would need to implement iteration, most commonly a List, Tuple, or less commonly an Array, or some custom iterator implementation.

In this specific case the error happens when the classic string interpolation goes to fill the %s, %d, %b string formatters.

Related:

Answered By: jmunsch

Pass parameter into a list, which is indexable.

cur.execute("select * from tableA where id =%s",[parameter])
Answered By: Ankit Singh Gulia

I had the same problem and it worked when I used normal formatting.

cursor.execute(f'
    SELECT "Indicator"."indicator" 
    FROM "Indicator" 
    WHERE "Indicator"."some_id" ={some_id};')
Answered By: H.asadi

Typecasting some_id to string also works.

cursor.execute(""" SELECT * FROM posts WHERE id = %s """, (str(id), ))
Answered By: stuckoverflow

Specify the vars= argument that might work:

cursor.execute("
          SELECT "Indicator"."indicator" 
          FROM "Indicator" 
          WHERE "Indicator"."some_id" = %s', vars=[id] )
Answered By: Malayaj Laad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.