Snowflake DWH: Connecting to Python

  • Published
  • Posted in Blog
  • Updated
  • 2 mins read

Hello readers, in my previous blog we’ve discussed how to create a database and a table within the database in Snowflake DWH. In this blog, we will see how to connect Snowflake DWH to Python.

Prerequisites: Make sure you have Python 3.x installed. If not, you can down it from here.

The Snowflake-Python Connector can be installed easily by using either the pip command or the conda installer.

Using pip command:

pip install –upgrade snowflake-connector-python

Using conda installer:

conda install -c conda-forge snowflake-connector-python

Now, we’re ready to go…

To connect Python to Snowflake, follow these steps-

  1. Import the snowflake.connector package

import snowflake. connector as snow

2. Connecting to Snowflake using login parameters-

#Connecting to Snowflake
conn = snow.connect(user=”UserName”
password=”Password” ,
account=”Account” ,
warehouse=”DWH-Name” ,
database=”DB-Name” ,
schema=”Schema”)

3. Creating a cursor-

curs=conn.cursor()

4. Execute the SQL query-

curs.execute(‘Select * from Your Table Name’)

This is how you can connect to Snowflake. Now, if you want to feed the data into Snowflake’s table you have to import another package i.e.

from snowflake.connector.pandas tools import write pandas

Once you have the above package imported in your code you can export pandas dataframe to Snowflake using below function:

#Posting Data to Snowfalke
#write pandas (ConnectionString, Dataframr, “SnowflakeTableName”)
write pndas (conn, df, “RETURNS”)

Conclusion

You have now learned how to install and use the Snowflake Connector for Python & the function you can use to post data to Snowflake’s table. 

Thank you for reading this blog. I hope you will find this blog helpful.

 

Gaurav Lakhotia
Data Analyst
Addend Analytics

Addend Analytics is a Microsoft Gold Partner based in Mumbai, India, and a branch office in the U.S.

Addend has successfully implemented 100+ Microsoft Power BI and Business Central projects for 100+ clients across sectors like Financial Services, Banking, Insurance, Retail, Sales, Manufacturing, Real estate, Logistics, and Healthcare in countries like the US, Europe, Switzerland, and Australia.

Get a free consultation now by emailing us or contacting us.