TypeError: 'int' object does not support indexing

typeerror python
typeerror is not a function
typeerror django
typeerror: is null
typeerror php
typeerror (ruby)
typeerror is undefined
typeerror: cancelled

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

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.

TypeError() constructor, The TypeError() constructor creates a new error when an operation could not be performed, typically (but not exclusively) when a value is not of  Type I Error: A Type I error is a type of error that occurs when a null hypothesis is rejected although it is true. The error accepts the alternative hypothesis

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

Built-in Exceptions, (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) exception EOFError ¶. Raised when the input() function  Introduction There are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type.

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

TypeError - Manual, The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only). Class synopsis ¶. TypeError extends Error {. /* Inherited  Type I errors are equivalent to false positives. Let’s go back to the example of a drug being used to treat a disease. If we reject the null hypothesis in this situation, then our claim is that the drug does, in fact, have some effect on a disease.

JavaScript Error Handling, A close look at the TypeError: null or undefined has no properties TypeError within JavaScript, including a quick look at null and undefined  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Node.js Error Handling - TypeError, A deep dive into the TypeError in Node.js, with sample code showing how both built-in Node API modules, along with custom modules, use  Understanding Type I and Type II Errors Hypothesis testing is the art of testing if variation between two sample distributions can just be explained through random chance or not. If we have to conclude that two distributions vary in a meaningful way, we must take enough precaution to see that the differences are not just through random chance.

TypeError: JavaScript, Per MDN: 'A TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or  The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions.

What is an uncaught TypeError in JavaScript?, According to the Mozilla website for developer documents, “the TypeError object Code. Let's go over some examples of the causes of the uncaught type error. A significance level α corresponds to a certain value of the test statistic, say t α, represented by the orange line in the picture of a sampling distribution below (the picture illustrates a hypothesis test with alternate hypothesis "µ > 0")

Comments
  • The comma makes ALL the difference. Didn't realize a single item tuple doesn't turn into a tuple without a comma...