psycopg2 execute_values - 使用多个占位符更新查询 - 抛出错误 - 列是日期类型但表达式是文本类型

问题描述 投票:0回答:0

我正在尝试复制@jjanes 提供的答案来处理

UPDATE
场景之一。

Psycopg2 执行值 - 查询包含多个 '%s' 占位符

我的代码如下:

UPDATE_QUERY = """UPDATE SHARE_RETURN_DOC
                                      SET TO_DB_TS = v1,
                                          SUPERSEDED_DT = v2,
                                          FIRST_SEEN_DT = v3,
                                          LAST_SEEN_DT = v4,
                                          BATCH_ID = v5,
                                          DATA_PROC_ID = v6,
                                          CO_REG_DEBT = v7,
                                          DIR_DTL_CHG_IN = v8,
                                          SHRHLDR_LIST_CD = v9,
                                          SHRHLDR_LIST_CD = v10,
                                          SHRHLDR_LEGAL_STAT = v11,
                                          SHRHLDR_REFRESH_CD = v12,
                                          SHRHLDR_SUPRESS_IN = v13,
                                          BULK_LIST_ID = v14,
                                          DOC_TYPE_CD = v15,
                                          JACKET_NR = v16
                                     FROM (VALUES %s) u(id1, id2, v1, v2, v3, v4, v5, v6, v7, v8, 
                                                        v9, v10, v11, v12, v13, v14, v15, v16)                                                    
                                    WHERE SHARE_RETURN_DOC.REG_NB = u.id1
                                      AND SHARE_RETURN_DOC.ANN_RTN_DT = u.id2"""

Method
执行
Update SQL

def tableUpdate(connection, cursor, query, dataframe):
    data = []
    dataframe = dataframe.toPandas()
    for x in dataframe.to_numpy():
        data.append(tuple(x))
    try:
        print(data)
        extras.execute_values(cursor, query, data)
        connection.commit()
    except (Exception, Error) as error:
        print("Error: %s" % error)
        connection.rollback()
        return 1
    finally:
        cursor.close()

我想传递给

data
extras.execute_value
是3个
list
tuples
组合。所以我在表中有 3 行到
Update
data
如下:

[('XXXXXXXX', datetime.date(2022, 12, 1), Timestamp('2023-03-16 09:51:31.634000'), **None**, datetime.date(2023, 3, 16), datetime.date(2023, 3, 16), '5e3642ee-3f91-4360-8694-d60e66317af2', 1, 0, 'N', '2', None, ' ', ' ', 'N', 'N', 'C', 'XXXWUC94'), ('ZZZZZZZZ', datetime.date(2022, 11, 1), Timestamp('2023-03-16 09:51:31.634000'), None, datetime.date(2023, 3, 16), datetime.date(2023, 3, 16), '5e3642ee-3f91-4360-8694-d60e66317af2', 1, 0, 'N', '2', None, ' ', ' ', 'N', 'N', 'C', 'ZZZWUC95'), ('YYYYYYYY', datetime.date(2022, 11, 21), Timestamp('2023-03-16 09:51:31.634000'), None, datetime.date(2023, 3, 16), datetime.date(2023, 3, 16), '5e3642ee-3f91-4360-8694-d60e66317af2', 1, 0, 'N', '7', None, ' ', ' ', 'N', 'N', 'C', 'YYYYY9D')]

但是,我得到一个

Error
,同时运行工作:

method
期待
Date
场。但输入数据行包含
**None**
.

column "superseded_dt" is of type date but expression is of type text
LINE 3: ...                                         SUPERSEDED_DT = v2,
                                                                    ^

我尝试了以下选项,导致了不同的其他错误。

SUPERSEDED_DT = v2::date[],
FROM (VALUES %s) u(id1, id2, v1, v2::date[], v3, v4, v5, v6, v7, v8,

你能帮我解决这个问题吗?

非常感谢!

python sql postgresql psycopg2
© www.soinside.com 2019 - 2024. All rights reserved.