Psygopg2 用 python 元组列表替换多列

Psygopg2 replacing multiple columns with python lists of tuples

我是 PostgreSQl 的新手。我在 PostgreSQL 9.5.6 中使用 Psycopg2。我的更新 SQL 现在看起来像这样。我想使用 Psycopg2.Python 元组列表更新多列。

update_sql = '''
            UPDATE my_table
            SET size = n, mean = m, std_pop = std
            FROM unnest(%s) s(my_id integer, n integer, m double precision, std double precision)
            WHERE my_table.id = s.my_id;
        '''

例如,我在 python 中有以下 Python 元组列表 (id, new_size, new_mean, new_std):

new_column_values =[(0,10,9.0,8.0),(1,10,9.0,8.0),(2,10,9.0,8.0)]

我想放入 table 并同时更新所有 3 列:

+----+------+------+---------+
| id | size | mean | std_pop |
+----+------+------+---------+
|  0 |    0 |    0 |       0 |
|  1 |    0 |    0 |       0 |
|  2 |    0 |    0 |       0 |
+----+------+------+---------+

这是更新多列的最佳方式吗?现在它 returns 一个错误:

psycopg2.ProgrammingError: function return row and query-specified return row do not match
DETAIL:  Returned type numeric at ordinal position 3, but query expects double precision.

说是double precision,结果是numeric类型,不知道怎么解决。我的 meanstd_pop 列是双精度列。这是怎么回事?

我解决了问题。

我刚刚将更新查询中的 double precision 更改为 numeric 并解决了问题。但是当我用 double precision.

声明列时,为什么我必须使用 numeric 是没有意义的
update_sql = '''
        UPDATE my_table
        SET size = n, mean = m, std_pop = std
        FROM unnest(%s) s(my_id integer, n integer, m numeric, std numeric)
        WHERE my_table.id = s.my_id;
    '''

也许有人可以解释为什么它必须是 numeric 而不是 double precision?