25 Nov Snowflake Connector for Python
Introduction
The snowflake connector is a native, pure Python package that does not depend on JDBC or ODBC. It can be installed using pip
on Linux, macOS, and Windows platforms where Python 3.6 and upwards are installed. The connector supports developing applications using the Python Database API v2 specification (PEP-249), including using the following standard API objects:
-
-
Cursor
objects for executing DDL/DML statements and queries.
How to install the Connector
Python Package Installer & Setup Tools
The Snowflake Connector for Python is installed with pip
, first execute the following command to ensure the required version is installed:
1
python -m pip install --upgrade pip
To install the Snowflake Connector for Python and the dependent libraries:
-
Determine the version of the Snowflake Connector for Python that you plan to install.
-
To install the dependent libraries, run the pip
(or pip3
) command and point to the requirement for that version of the connector.
1
pip install -r https://raw.githubusercontent.com/snowflakedb/snowflake-connector-python/v2.6.2/tested_requirements/requirements_36.reqs
Validate your installation
Create a file known as validate.py, and run the file which shows the version of the snowflakes. If this code brings up an error when ran, this shows that something is wrong with the installation.
#!/usr/bin/env python
import snowflake.connector
# Gets the version
ctx = snowflake.connector.connect(
user='john',
password='xxxxxxxx',
account='xxxxxxxx'
)
cs = ctx.cursor()
try:
cs.execute("SELECT current_version()")
one_row = cs.fetchone()
print(one_row[0])
finally:
cs.close()
ctx.close()
How to Use the Python Connector
You can Connect to Snowflake using the login parameters:
import snowflake.connector
PASSWORD = os.getenv('SNOWSQL_PWD')
WAREHOUSE = os.getenv('WAREHOUSE')
conn = snowflake.connector.connect( user=USER,
password=PASSWORD,
account=ACCOUNT,
warehouse=WAREHOUSE,
database=DATABASE,
schema=SCHEMA )
After you have logged in, create a database, schema, and warehouse with the connection.
How to Create Database, Schema, and Warehouse
You can use the CREATE DATABASE, CREATE SCHEMA and CREATE WAREHOUSE commands to create this. I displayed an example below for each, whereby I specified the name of the database in which to create the schema and also a name specified for the data warehouse.
conn.cursor().execute("CREATE WAREHOUSE IF NOT EXISTS john_warehouse")
conn.cursor().execute("CREATE DATABASE IF NOT EXISTS testing")
conn.cursor().execute("USE DATABASE testing")
conn.cursor().execute("CREATE SCHEMA IF NOT EXISTS testingschema")
Using the Database, Schema, and Warehouse
You can also reference your database, schema, and warehouse. Make use of each with the following commands with which we can start creating tables afterward.
conn.cursor().execute("USE WAREHOUSE tiny_warehouse_mg")
conn.cursor().execute("USE DATABASE testdb_mg")
conn.cursor().execute("USE SCHEMA testdb_mg.testschema_mg")
How to Create Tables
You can create tables and insert data into your database using the connection. Check the example below.
onn.cursor().execute(
"CREATE OR REPLACE TABLE "
"first_t(col1 integer, col2 string)")
conn.cursor().execute(
"INSERT INTO first_t(col1, col2) VALUES " +
" (123, 'test string1'), " +
" (456, 'test string2')")
Hope you found this article helpful. If you have any questions, feel free to ask in the comments section.
No Comments