TypeError:“ int”对象不支持索引 [英] TypeError: 'int' object does not support indexing
问题描述
我有这个查询:
some_id = 1
cursor.execute('
SELECT "Indicator"."indicator"
FROM "Indicator"
WHERE "Indicator"."some_id" = %s;', some_id)
我收到以下错误:
TypeError: 'int' object does not support indexing
some_id是一个int,但我想选择具有some_id = 1(或任何我决定放入变量的#)。
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])
这会将 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.
由于该方法中的某处可能正在尝试遍历该输入而发生错误,或者直接索引到它。可能是这样的: some_id [0]
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.
您还可以通过执行以下操作将其变成元组:(some_id,)
具有不变的优点。
You could also make it into a tuple by doing this: (some_id,)
which has the advantage of being immutable.
这篇关于TypeError:“ int”对象不支持索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!