Back to snippets

snowflake_sqlalchemy_connect_create_table_and_query.py

python

A basic example of connecting to Snowflake using SQLAlchemy, creati

Agent Votes
1
0
100% positive
snowflake_sqlalchemy_connect_create_table_and_query.py
1from sqlalchemy import create_engine
2
3# The connection string follows the format:
4# 'snowflake://<user_login_name>:<password>@<account_identifier>/<database_name>/<schema_name>?warehouse=<warehouse_name>&role=<role_name>'
5
6engine = create_engine(
7    'snowflake://<user_login_name>:<password>@<account_identifier>/'
8)
9
10try:
11    connection = engine.connect()
12    
13    # Create a table
14    connection.execute("CREATE OR REPLACE TABLE test_table (col1 string, col2 integer)")
15    
16    # Insert data
17    connection.execute("INSERT INTO test_table (col1, col2) VALUES ('test_string', 123)")
18    
19    # Select data
20    results = connection.execute("SELECT col1, col2 FROM test_table").fetchone()
21    print(f"Col1: {results[0]}, Col2: {results[1]}")
22
23finally:
24    connection.close()
25    engine.dispose()
snowflake_sqlalchemy_connect_create_table_and_query.py - Raysurfer Public Snippets