I saved some queries in a single .sql script, instead of writing them directly inside variables in a .py script.
Now I want to read this sql script using Python, split each sql statement by semicolon and then pass each of these scripts to Psycopg2 cursor for sequential execution.
The python script seems to read the file correctly, but a "can't execute an empty query" error is thrown when I try to execute the statements.
Maybe the catch is: there are many line-breaks spread across this sql script. Statements in it are written like below:
DROP TABLE IF EXISTS
target_schema.some_table
;
SELECT
column
FROM
schema.table
;
Here's the python code:
import psycopg2
import pathlib
conn = psycopg2.connect(
user=user
,password=password
,host=host
,port=port
,database=database
)
pg_cursor = conn.cursor()
scriptContents = Path('my_folder\my_sql_script.sql').read_text(encoding='utf-8')
sqlStatements = scriptContents.split(sep=';')
for statement in sqlStatements:
try:
pg_cursor.execute(f'{statement}')
conn.commit()
except psycopg2.Error as errorMsg:
print(errorMsg)
conn.rollback()
Could anyone help me solve this?