python - 在 python psycopg2 中通过列名检索值

标签 python python-3.x psycopg2

>>> import psycopg2
>>> tb = 'races';
>>> conn2 = psycopg2.connect(host='localhost',dbname=latest_prod_copy,port=5438)
>>> cursor2.execute(sql.SQL("SELECT * from {}").format(sql.Identifier(tb)))
>>> cursor2.description[0]
Column(name='id', type_code=23, display_size=None, internal_size=4, precision=None, scale=None, null_ok=None)
>>> cursor2.description[0][0]
'id'
>>> cursor2.description[0][1]
23

我的问题是如何通过键检索值?例如cursor2.description[0]->'type_code',而不是cursor2.description[0][1]

http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.Column http://initd.org/psycopg/docs/cursor.html#cursor.description

最佳答案

cursor2.description[0] 是一个 psycopg2.extensions.Column 对象,您可以通过名称访问其字段,例如

cursor2.description[0].type_code

关于python - 在 python psycopg2 中通过列名检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53750677/

相关文章:

Python Psycopg2 For 循环,大型数据库问题

python - 如何使用 psycopg2 更新 postgresql 中多行的多列

python - 查询在 psql 中有效,但在使用 psycopg2 的 Python 中出现语法错误

python - 在 Tkinter 中加载图像的角落

python - 如何在地理边界内生成随机经纬度点?

python - 如何在正在播放的pygame中获取音乐的位置?

python - Databricks - pyspark.pandas.Dataframe.to_excel 无法识别 abfss 协议(protocol)

python - Tkinter 与 Python 3.3 : Change colour of button on click

python - 在 Python 3.7 中将函数声明为返回类型

python - 如何将字符串中的 "\t"拆分为两个单独的字符 "\"和 "t"? (如何拆分转义序列?)