Skip to content Skip to sidebar Skip to footer

Attributeerror: 'snowflakecursor' Object Has No Attribute 'cursor'

I am trying to writer my DataFrame to Snowflake using to_sql method. sf_conn = snowflake.connector.connect( account=*****, user=*****, password=*****, role=*****,

Solution 1:

You need to use an SQLAlchemy engine as the connection when using pandas.DataFrame.to_sql with Snowflake.

When you use df.to_sql, you need to pass in a SQLAlchemy engine and not a standard Snowflake connection object (and not a cursor either as you've tried to do). You'll need to install snowflake-sqlalchemy using pip but you don't need to install snowflake-connector-python since the snowflake-sqlalchemy does this for you.

Here is an example that is working for me:

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

Every time I run the above script the Mark and Luke records get appended to my test_db.public.test_table table.

Post a Comment for "Attributeerror: 'snowflakecursor' Object Has No Attribute 'cursor'"