postgresql psycopg 2 execute_values -使用多个保持器更新查询-抛出错误-列的类型为date,但表达式的类型为text

rsaldnfx  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(263)

我试图复制@jjanes提供的答案来处理其中一个UPDATE场景。
Psycopg2 execute values -the query contains more than one '%s' placeholder
代码如下:

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

我试图传递给extras.execute_valuedata是3个tuples组合的list。所以我在表中有3行到Updatedata如下:

[('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,
你能帮我解决这个问题吗?
非常感谢!

dxpyg8gm

dxpyg8gm1#

当你设置要更新的列时,将临时表列的类型转换为原始表列的类型,因为在临时表中,每一列都是text类型。
例如:

UPDATE_QUERY = """UPDATE SHARE_RETURN_DOC
                              SET TO_DB_TS = v1,
                                  SUPERSEDED_DT = v2::timestamp without time zone,
                                  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"""

相关问题