Snowflake SQLAlchemy Dialect

1.9.0 · active · verified Sat Mar 28

The Snowflake SQLAlchemy Dialect is a Python library that provides a SQLAlchemy dialect for connecting to Snowflake data warehouses. It functions as a bridge between SQLAlchemy applications and the Snowflake Connector for Python. The library is actively maintained with frequent releases, often multiple times per quarter, to introduce new features, improve compatibility, and address bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to Snowflake using `create_engine` with the `snowflake.sqlalchemy.URL` object. It then executes a simple query to retrieve the current Snowflake version. Remember to replace placeholder credentials with your actual Snowflake account details or set them as environment variables. It's crucial to properly close the connection and dispose of the engine.

import os
from sqlalchemy import create_engine, text
from snowflake.sqlalchemy import URL

# Replace with your Snowflake credentials or set them as environment variables
SNOWFLAKE_ACCOUNT = os.environ.get('SNOWFLAKE_ACCOUNT', 'your_account_identifier')
SNOWFLAKE_USER = os.environ.get('SNOWFLAKE_USER', 'your_user_login_name')
SNOWFLAKE_PASSWORD = os.environ.get('SNOWFLAKE_PASSWORD', 'your_password')
SNOWFLAKE_WAREHOUSE = os.environ.get('SNOWFLAKE_WAREHOUSE', 'your_warehouse')
SNOWFLAKE_DATABASE = os.environ.get('SNOWFLAKE_DATABASE', 'your_database')
SNOWFLAKE_SCHEMA = os.environ.get('SNOWFLAKE_SCHEMA', 'your_schema')

try:
    engine = create_engine(
        URL(
            account=SNOWFLAKE_ACCOUNT,
            user=SNOWFLAKE_USER,
            password=SNOWFLAKE_PASSWORD,
            database=SNOWFLAKE_DATABASE,
            schema=SNOWFLAKE_SCHEMA,
            warehouse=SNOWFLAKE_WAREHOUSE
        )
    )

    with engine.connect() as connection:
        result = connection.execute(text('SELECT current_version()')).fetchone()
        print(f"Connected to Snowflake. Current version: {result[0]}")

except Exception as e:
    print(f"Error connecting to Snowflake: {e}")
finally:
    if 'engine' in locals():
        engine.dispose() # Ensures all connections are closed.

view raw JSON →