使用字典插入 psycopg 中的大 table

Insert into a large table in psycopg using a dictionary

我在数据库中有一个非常大的 table(>200 列),我正在通过 psycopg2 访问它。我有要作为字典插入的行,列名作为键,值作为值。使用 psycopg2,我想将该行插入 table.

由于所讨论的 table 的列数过多,我宁愿不手动编写插入语句。如何高效整齐地插入字典?

这是测试table:

create table testins (foo int, bar int, baz int)

您可以这样编写 sql 语句:

d = dict(foo=10,bar=20,baz=30)

cur.execute(
    "insert into testins (%s) values (%s)" 
        % (','.join(d), ','.join('%%(%s)s' % k for k in d)),
    d)