I have a parameterized query to be executed on a Postgres database:
SELECT
DISTINCT ON (customer_id) customer_id,
COALESCE('Terminated') AS note,
TO_CHAR((DATE_TRUNC('DAY', note_date)), 'YYYY-mm-dd') AS note_date
FROM
notes
WHERE
note LIKE 'Terminated.%'
AND note_date = %s
LIMIT
1
I'm running the query in python:
def execute_query(creds, sql_file_path, param1):
with create_db_conn(creds) as cursor:
sql_file = open(sql_file_path, "r")
query = sql_file.read()
try:
cursor.execute(query, (param1,))
except psycopg2.DatabaseError as error:
print(f"Query failed to execute: {error}")
# Fetch all results
data = cursor.fetchall()
return data
When I run this query, I get the following error message:
Traceback (most recent call last):
File "/__main__.py", line 64, in execute_query
cursor.execute(query, (param1,))
IndexError: tuple index out of range
The query runs successfully when I remove the pattern match subclause note LIKE 'Terminated.%'. How do I fix this error while keeping the pattern match subclause in the query? The psycopg2 docs don't describe alternative placeholders other than %s. Thanks in advance for any input or resources.