TypeError: 'int' object does not support indexing

PythonSqlPostgresql

Python Problem Overview


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).

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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,))

Solution 3 - Python

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, ))

Solution 4 - Python

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:

Solution 5 - Python

Pass parameter into a list, which is indexable.

cur.execute("select * from tableA where id =%s",[parameter])

Solution 6 - Python

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};')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnlr25View Question on Stackoverflow
Solution 1 - PythonStephanView Answer on Stackoverflow
Solution 2 - PythonalecxeView Answer on Stackoverflow
Solution 3 - PythonpmcnameeView Answer on Stackoverflow
Solution 4 - PythonjmunschView Answer on Stackoverflow
Solution 5 - PythonAnkit Singh GuliaView Answer on Stackoverflow
Solution 6 - PythonH.asadiView Answer on Stackoverflow