0% found this document useful (0 votes)
3 views

Python+Code

The document is a Python script that connects to a Snowflake database using user credentials. It executes SQL commands to set the warehouse, resume it if necessary, and select data from a 'healthcare' table, which is then loaded into a Pandas DataFrame. The script also includes error handling for connection and execution issues.

Uploaded by

AFZAL KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python+Code

The document is a Python script that connects to a Snowflake database using user credentials. It executes SQL commands to set the warehouse, resume it if necessary, and select data from a 'healthcare' table, which is then loaded into a Pandas DataFrame. The script also includes error handling for connection and execution issues.

Uploaded by

AFZAL KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import snowflake.

connector as sf
import pandas as pd
import getpass

password = getpass.getpass("Enter your snowflake password: ")

# make changes as per your credentials


user='kashishgakkar'
account='eca91330.us-east-1'
database='DEMO_DB'
warehouse='COMPUTE_WH'
schema='PUBLIC'
role='ACCOUNTADMIN'

conn = sf.connect(user = user,


password = password,
account = account,
)

def run_query(connection,query):
cursor = connection.cursor()
cursor.execute(query)
cursor.close()

sql = f'use warehouse {warehouse}'


run_query(conn, sql)

try:
warehouse_sql = 'use warehouse {}'.format(warehouse)
run_query(conn, warehouse_sql)

try:
sql = 'alter warehouse {} resume'.format(warehouse)
run_query(conn, sql)
except:
pass

sql = 'use database {}'.format(database)


run_query(conn, sql)

sql = 'use role {}'.format(role)


run_query(conn, sql)

sql = f'use schema {schema}'


run_query(conn, sql)

except Exception as e:
print(e)

sql = 'select * from healthcare limit 20'


df = pd.read_sql(sql, conn)

df.tail(10)

You might also like