I have seen a similar question with answers here How can I print literal curly-brace characters in a string and also use .format on it?
But he is not suitable for this case:
I run an SQL query as a string and place variables in it. The problem is that inside a query there is a symbol like "}" and then I get an error like:
Single '}' encountered in the format string.
as example:
import psycopg2
import pandas as pd
conn = psycopg2.connect(host="xxx", port = 5432, database="xxxx", user="xxx", password="xxxx")
statment= """
Select User_name,
replace(replace(product_name,'{',''),'}','') as product_name,
date
from Sales
where user_id={v_User_ID} and shop_id={v_shop_id}
UNION
Select User_name,
replace(replace(product_name,'{',''),'}','') as product_name,
date
from Sales
where shop_id={v_shop_id} and country="France"
"""
test=statment.format(v_User_ID=181 ,v_shop_id=107)
df= pd.read_sql_query(test ,con=conn)
df.head()
When I run a script without the "}" icon, everything is fine, but I must use this icon within the SQL query.
How can this problem be solved?