gpt4 book ai didi

database - psycopg2 - 类型错误 : 'int' object is not subscriptable

转载 作者:搜寻专家 更新时间:2023-10-30 23:34:51 25 4
gpt4 key购买 nike

我正在使用存储在函数 columnsearch() 中的 psycopg2 执行 postgres 查询。我的目标是能够通过返回语句中的列名来查看每个字段中的数据。我收到错误:

TypeError: 'int' object is not subscriptable

有谁知道我该如何纠正这个问题?

def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("SELECT * FROM radio_archive_ind.radio_archive_ind WHERE tape_number = 'TP00001'")
rows = cur.fetchone()
for r in rows:
return r["tape_number"]

print columnsearch()

最佳答案

注意 .fetchone() 返回 a single sequence所以你可以这样做:

def columnsearch():
con = psycopg2.connect(connectstring)
cur = con.cursor(cursor_factory=e.DictCursor)
cur.execute("""SELECT *
FROM radio_archive_ind.radio_archive_ind
WHERE tape_number = 'TP00001'""")
row = cur.fetchone()
return row["tape_number"]

关于database - psycopg2 - 类型错误 : 'int' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44440113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com