如何使用python在snowflake中进行查询(sso身份验证)?

jaql4c8m  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(462)

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

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) 
'''
ki0zmccv

ki0zmccv1#

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

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-用于获取值的游标

jjhzyzn0

jjhzyzn02#

查询结果存储在游标中。游标的内容可以存储在局部变量中。
此外,最好的做法,以密切联系在最后。
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()

相关问题