0
votes

How do we connect to snowflake using python connector and execute multiple sql statements, instead of writing multiple cursor execute()?

If we have a sql script, can we run the script by using the python snowflake connector? is there a better way to connect to snowflake?

import snowflake.connector
ctx=snowflake.connector.connect(
   user=user,
   password=password,
   account=account
)
cs=ctx.cursor()
query="select * from table;"//sql query like select *
try:
   cs.execute(query)
   output=cs.fetchall()
   print(output)
finally:
   cs.close()
ctx.close()
1
Do not open and close a connection for every query. Just open and close the cursor. You have to open cursor, run the query, get the results, close the cursor for each query. - Neeraj Agarwal
Thank you for your response. - zoref
If we need to execute 3 queries, say q1, q2, q3 do we need to have 3 cursors? or cs.execute(q1) cs.execute(q2) cs.execute(q3) is correct for cursor cs? - zoref
A cursor can execute only one query at a time. If you are executing q1, q2,q3 sequentially then open cursor c1, execute q1, get all results, execute q2, get all results, execute q3, get all results, close cursor c1. If you executing in parallel then you need to open cursor c1 for q1, c2 for q2 and c3 for q3. You may have to check the snowflake documentation on how they have implemented cursors in a database connection, - Neeraj Agarwal

1 Answers

-1
votes

For true parallel execution, you can implement a multi-threaded routine that launches jobs for simultaneous execution.