AttributeError:'SnowflkeCursor'对象没有属性'光标' [英] AttributeError: 'SnowflakeCursor' object has no attribute 'cursor'

查看:54
本文介绍了AttributeError:'SnowflkeCursor'对象没有属性'光标'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TO_SQL方法将DataFrame写入Snowflake。

sf_conn = snowflake.connector.connect(
    account=*****,
    user=*****,
    password=*****,
    role=*****,
    warehouse=*****,
    database=*****
    
)

sf_cur = sf_conn.cursor()
df = pd.DataFrame([('Mark', 10), ('Luke', 20)], columns=['name', 'balance'])
df.to_sql('TEST3',con=sf_cur, schema='public', index=False)

但还没有运气。

File "/home/karma/.local/lib/python3.6/site-packages/pandas/io/sql.py", line 1584, in execute
    cur = self.con.cursor()
AttributeError: 'SnowflakeCursor' object has no attribute 'cursor'

我甚至尝试了con=sf_conn,但得到以下错误:

pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting

我可以使用sqlAlChemy create_engine lib执行相同的工作,但希望专门使用雪花连接。

推荐答案

pandas.DataFrame.to_sql与Snowflake一起使用时,需要使用SQLAlChemy引擎作为连接。

当您使用df.to_sql时,您需要传入一个SQLAlChemy引擎,而不是一个标准的Snowflake连接对象(也不是您尝试过的游标)。您将需要使用pip安装snowflake-sqlalchemy,但您不需要安装snowflake-connector-python,因为雪花-sqlalChemical会为您执行此操作。

这里有一个适用于我的示例:

from sqlalchemy import create_engine
import os
import pandas as pd

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 = 'test_db'
snowflake_schema = 'public'


if __name__ == '__main__':
    engine = create_engine(
        'snowflake://{user}:{password}@{account}/{db}/{schema}?warehouse={warehouse}'.format(
            user=snowflake_username,
            password=snowflake_password,
            account=snowflake_account,
            db=snowflake_database,
            schema=snowflake_schema,
            warehouse=snowflake_warehouse,
        )
    )
    df = pd.DataFrame([('Mark', 10), ('Luke', 20)], columns=['name', 'balance'])
    df.to_sql('TEST_TABLE', con=engine, schema='public', index=False, if_exists='append')

每次我运行上述脚本时,Mark和Luke记录都会追加到我的test_db.public.test_table表中。

这篇关于AttributeError:'SnowflkeCursor'对象没有属性'光标'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆