You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code中Snowflake连接器调用cursor()报AttributeError,Jupyter可正常运行

Snowflake连接脚本报错AttributeError问题解决

问题背景

Jupyter Notebook中通过snowflake.connector连接Snowflake,调用conn.cursor()执行SQL拉取数据完全正常,但将代码封装为类移到VS Code中运行时,抛出错误:

AttributeError: 'SnowflakeConnection' object has no attribute 'cusror'

仅需拉取数据转为DataFrame,疑惑为何环境不同结果不同。

Jupyter正常运行代码

conn = snowflake.connector.connect(<connection parameters>)         

ib_cursor = conn.cursor()
try: 
    ib_cursor.execute(ib_sql.read(), params)
    ib_rows = ib_cursor.fetchall()

VS Code报错脚本代码

class snowflk_sql:
    def __init__(self, conn_parameters):
        self.connection_params = conn_parameters
        self.connection_obj  = snowflake.connector.connect(user= self.connection_params['user'],
                                        account= self.connection_params['account'],
                                        role= self.connection_params['role'],
                                        database= self.connection_params['database'],
                                        schema = self.connection_params['schema'],
                                        authenticator="externalbrowser",
                                        autocommit=True) 

    def sql_execute(self,parameter):
        ib_cursor = self.connection_obj.cursor() 
        so_cursor = self.connection_obj.cusror() 
        
        #.......<some logic>......

        try:
            # sql being executed using parameters in the parameter file
            ib_cursor.execute(ib_sql.read(), parameter) 
            ib_rows = ib_cursor.fetchall()
# rest of the code

def main(connection_param,parameter):
    # creating the snowflake connection
    snowflk_sql_obj = snowflk_sql(connection_param)
    # executing the sql 
    ib_df,so_df = snowflk_sql_obj.sql_execute(q_parameter)

问题原因及解决

原因:拼写错误
VS Code脚本中so_cursor = self.connection_obj.cusror()这一行,把cursor拼成了cusror,这是导致AttributeError的直接原因。Jupyter代码里没有这个拼写错误,所以能正常运行。

修正后的代码片段

def sql_execute(self,parameter):
    ib_cursor = self.connection_obj.cursor() 
    so_cursor = self.connection_obj.cursor()  # 修正拼写错误
    
    #.......<some logic>......

    try:
        ib_cursor.execute(ib_sql.read(), parameter) 
        # 直接转为DataFrame的简化方式(无需手动处理fetchall)
        ib_df = ib_cursor.fetch_pandas_all()
# rest of the code

额外优化提示

如果需要将查询结果转为DataFrame,无需先fetchall再手动构造DataFrame,直接调用游标对象的fetch_pandas_all()方法即可一步完成,代码更简洁高效。

内容的提问来源于stack exchange,提问作者haldar55

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 02:02:21