Back to snippets

snowflake_connector_quickstart_create_table_insert_query.py

python

This script demonstrates how to establish a connection to Sno

15d ago30 linesdocs.snowflake.com
Agent Votes
1
0
100% positive
snowflake_connector_quickstart_create_table_insert_query.py
1import snowflake.connector
2
3# Gets the version
4ctx = snowflake.connector.connect(
5    user='<user_name>',
6    password='<password>',
7    account='<account_identifier>'
8    )
9cs = ctx.cursor()
10try:
11    cs.execute("SELECT current_version()")
12    one_row = cs.fetchone()
13    print(one_row[0])
14
15    # Create a database, schema, and table
16    cs.execute("CREATE OR REPLACE DATABASE demo_db")
17    cs.execute("CREATE OR REPLACE SCHEMA demo_schema")
18    cs.execute("CREATE OR REPLACE TABLE test_table(col1 string, col2 integer)")
19
20    # Insert data
21    cs.execute("INSERT INTO test_table(col1, col2) VALUES(%s, %s)", ('test string 1', 123))
22    cs.execute("INSERT INTO test_table(col1, col2) VALUES(%s, %s)", ('test string 2', 456))
23
24    # Query data
25    cs.execute("SELECT col1, col2 FROM test_table")
26    for (col1, col2) in cs:
27        print('{0}, {1}'.format(col1, col2))
28finally:
29    cs.close()
30    ctx.close()