【问题标题】:How to querying in Snowflake using Python (SSO Authentication)?如何使用 Python(SSO 身份验证)在 Snowflake 中进行查询?
【发布时间】:2020-05-28 12:19:06
【问题描述】:

我尝试连接雪花(SSO 身份验证)并从表中获取数据。 但是,当我运行代码时,我可以在弹出的浏览器窗口中使用我的凭据登录并连接雪花,之后没有响应(程序既不终止也不提供结果)。 不知道哪里做错了,请帮忙。

'''

import snowflake.connector
# Connecting to Snowflake using SAML 2.0-compliant IdP federated authentication
conn = snowflake.connector.connect(
  user='G*****K',
  account='abcdf',
  authenticator='externalbrowser',
  warehouse='abcdf',
  database='abcdf',
  schema='abcdf'
)
cur = conn.cursor()
sql = "select * from abcdf.ACCT limit 10"
x=cur.execute(sql)
cur.close()
print(x) 
'''

【问题讨论】:

  • 你能试试其他命令吗,比如“Select current_date;”

标签: python sql snowflake-cloud-data-platform snowflake-task


【解决方案1】:

我相信你在打印之前关闭了光标;

    try:
    cur.execute("SELECT col1, col2 FROM test_table ORDER BY col1")
    for (col1, col2) in cur:
        print('{0}, {1}'.format(col1, col2))
    finally:
    cur.close() 

详情:https://docs.snowflake.com/en/user-guide/python-connector-example.html#using-cursor-to-fetch-values

【讨论】:

    【解决方案2】:

    查询结果存储在游标中。然后可以将游标的内容存储在局部变量中。

    另外,最后关闭连接的最佳实践。

    https://www.psycopg.org/docs/cursor.html

    import snowflake.connector
    # Connecting to Snowflake using SAML 2.0-compliant IdP federated authentication
    conn = snowflake.connector.connect(
      user='G*****K',
      account='abcdf',
      authenticator='externalbrowser',
      warehouse='abcdf',
      database='abcdf',
      schema='abcdf'
    )
    cur = conn.cursor()
    sql = "select * from abcdf.ACCT limit 10"
    cur.execute(sql)
    print(cur.fetchall())
    cur.close()
    conn.close()
    

    【讨论】:

      猜你喜欢
      • 2021-03-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      相关资源
      最近更新 更多