python + psycopg2.errors.SyntaxError: syntax error at end of input

Question:

I’m getting this error message

 cursor.execute(query, variables)
psycopg2.errors.SyntaxError: syntax error at end of input

My code

data = {
    'country': data['country'][x],
    'year': data['year'][x].astype(float),
    'month': data['month'][x].astype(float)
}
db_connection.execute(
    f"""
    INSERT INTO my_table (country, year, month) VALUES (%(country)s, %(year)s, %(month)s) ON CONFLICT (country, year, month)
    """,
    data,
)
Asked By: Nurdin

||

Answers:

You’re missing a conflict_action in your sql statement. See https://www.postgresql.org/docs/current/sql-insert.html#:~:text=ON%20CONFLICT%20DO%20NOTHING%20simply,can%20perform%20unique%20index%20inference. for details.

EG You might want ON CONFLICT (country, year, month) DO NOTHING

Answered By: TerryA
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.