Update table
Tags: #snowflake #database #snippet
Author: Sanjay Sabu

Input

Import libraries

1
import csv
2
from snowflakeconnector import SnowflakeConnector
Copied!

Insert your credentials

1
username = "sanjaynaas"
2
password = "Password123"
Copied!

Specify your account details

1
account = "iz84541.europe-west4.gcp"
2
database = "DEMO_DB"
3
table_name = "NAAS"
Copied!

Model

Read the csv file and converting to list

1
with open('Excel-Sales_Feb2020.csv') as f:
2
reader = csv.reader(f)
3
d_list = list(reader)
4
d_list
Copied!

Connecting to Snowflake

1
#Initialize SnowflakeConnector
2
instance = SnowflakeConnector(username,password,account,database)
Copied!

Updating Snowflake

1
for i in range(1,len(d_list)):
2
table_insert_csv_query= "INSERT INTO NAAS values"
3
table_insert_csv_query = table_insert_csv_query +str(tuple(d_list[i]))
4
instance.execute_query(table_insert_csv_query,query_type="push")
Copied!

Output

To see contents of table

1
table_display_query = "select * from " + table_name
2
#Fetch records from Snowflake database
3
instance.execute_query(table_display_query,query_type="pull")
Copied!

Closing the Connection

1
instance.close_connection()
Copied!

Display result

Copy link
Edit on GitHub