【问题标题】:python + psycopg2.errors.SyntaxError: syntax error at end of inputpython + psycopg2.errors.SyntaxError:输入末尾的语法错误
【发布时间】:2022-12-03 07:39:08
【问题描述】:

我收到此错误消息

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

我的代码

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,
                )

【问题讨论】:

    标签: python postgresql


    【解决方案1】:

    您的 sql 语句中缺少 conflict_action。见https://www.postgresql.org/docs/current/sql-insert.html#:~:text=ON%20CONFLICT%20DO%20NOTHING%20simply,can%20perform%20unique%20index%20inference。了解详情。

    EG 你可能想要ON CONFLICT (country, year, month) DO NOTHING

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2019-02-20
      相关资源
      最近更新 更多