简体   繁体   中英

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=*****,
    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)

But no luck yet.

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'

Even tried giving con=sf_conn but get the following error:

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

I am able to do the same job using sqlAlchemy create_engine lib, but wanted to use specifically snowflake connection.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM