gpt4 book ai didi

python - 使用 python 雪花连接器从雪花表获取列标题

转载 作者:行者123 更新时间:2023-12-03 08:20:41 26 4
gpt4 key购买 nike

如何使用雪花连接器游标获取表数据以及列名称。好吧,我可以使用 dictcursor 获取它,但是合并结果集变得很复杂,因为它将所有数据作为 key 对提供。我想知道是否有任何直接的方法。

最佳答案

要获取列标题,您可以使用光标的 description 属性,该属性返回结果的元数据并进行描述 here .

像下面这样使用它:

import snowflake.connector
import os

snowflake_username = os.environ['SNOWFLAKE_USERNAME']
snowflake_password = os.environ['SNOWFLAKE_PASSWORD']
snowflake_account = os.environ['SNOWFLAKE_ACCOUNT']
snowflake_warehouse = os.environ['SNOWFLAKE_WAREHOUSE']
snowflake_database = 'sample_data'
snowflake_schema = 'tpch_sf1'


if __name__ == '__main__':
with snowflake.connector.connect(
user=snowflake_username,
password=snowflake_password,
account=snowflake_account,
warehouse=snowflake_warehouse,
database=snowflake_database,
schema=snowflake_schema,
autocommit=False
) as con:
# Execute cursor and print metadata
cursor = con.cursor().execute("select * from sample_data.TPCH_SF1.customer limit 10")
for c in cursor.description:
print(c)

# # Fetch and print results
# results = cursor.fetchall()
# print(results)

上面的打印结果:

('C_CUSTKEY', 0, None, None, 38, 0, False)
('C_NAME', 2, None, 25, None, None, False)
('C_ADDRESS', 2, None, 40, None, None, False)
('C_NATIONKEY', 0, None, None, 38, 0, False)
('C_PHONE', 2, None, 15, None, None, False)
('C_ACCTBAL', 0, None, None, 12, 2, False)
('C_MKTSEGMENT', 2, None, 10, None, None, True)
('C_COMMENT', 2, None, 117, None, None, True)

关于python - 使用 python 雪花连接器从雪花表获取列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67945837/

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