4

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.

1 Answer 1

5

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:

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.