Python 类型错误:“int”对象不支持索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18345825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:31:45  来源:igfitidea点击:

TypeError: 'int' object does not support indexing

pythonsqlpostgresql

提问by nlr25

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

some_id 是一个 int 但我想选择具有 some_id = 1(或我决定放入变量的任何指标)的指标。

采纳答案by Stephan

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

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

这会将some_id参数转换为可索引的列表。假设您的方法像我认为的那样有效,这应该有效。

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]

发生错误是因为在该方法中的某个地方,它可能试图迭代该输入,或直接对其进行索引。可能是这样的: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.

你也可以通过这样做将它变成一个元组:(some_id,)它具有不可变的优点。

回答by alecxe

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

您应该将查询参数execute()作为元组(严格来说是可迭代的)传递给,(some_id,)而不是some_id

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

回答by pmcnamee

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:

您的 id 需要某种可迭代的 mogrify 才能理解输入,这是常见问题文档中的相关引用:

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

回答by jmunsch

Slightly similar error when using Django:

使用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.executewould need to implement iteration, most commonly a List, Tuple, or less commonly an Array, or some custom iterator implementation.

基本上,错误读取Some type xyz doesn't have an __ iter __ or __next__ or next function, so it's not next(), or itsnot[indexable], or iter(itsnot),在这种情况下, 的参数cursor.execute需要实现迭代,最常见的是 a ListTuple,或者不太常见的是 an Array,或者一些自定义迭代器实现。

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

在这种特定情况下,当经典字符串插值填充%s, %d,%b字符串格式化程序时会发生错误。

Related:

有关的: