How to connect to snowflake using multiple SQL code in Python vscode?

376 Views Asked by At

I am trying to connect to the snowflake database using Python. I have the .sql file in VS code that contains multiple SQL statements. For example:

select * from table1;
select * from table2:
select * from table3: 

So, I tried this code to get the result but it returned an error:

"Multiple SQL statements in a single API call are not supported; use one API call per statement instead."

My Python code is

#!/usr/bin/env python
import snowflake.connector

# Gets the version
ctx = snowflake.connector.connect(
    user='<user_name>',
    password='<password>',
    account='<account_identifier>'
    )
cs = ctx.cursor()
try:
    with open('<file_directory>') as f:
        lines = f.readlines()

    cs.execute(lines)
    data_frame=cs.fetch_pandas_all()
    data_frame.to_csv('filename.csv')
finally:
    cs.close()
ctx.close()

What can I try next?

1

There are 1 best solutions below

0
user3136936 On

Perhaps do as the error suggest, and limit each API call to a single SQL statement?

dfs = []
for line in lines:
    cs.execute(lines)
    dfs.append(cs.fetch_pandas_all())
df = pd.concat(dfs).to_csv('filename.csv')