简体   繁体   English

将字典的Python列表插入PSQL数据库

[英]Insert Python list of dictionaries into PSQL database

I have a list of dictionaries with the same keys (built in Python) which I would like to insert into a PSQL database. 我有一个字典列表,这些字典具有相同的键(用Python内置),我想将其插入PSQL数据库。 Key names coincide with the columns of the PSQL database. 键名与PSQL数据库的列一致。 Could someone please suggest something to do it efficiently? 有人可以建议一些有效的方法吗?

You can use .executemany method. 您可以使用.executemany方法。 Example using psycopg2 : 使用psycopg2示例:

cursor.executemany(
    'INSERT INTO mytable (field_a, field_b, filed_c) '
    'VALUES (%(field_a)s, %(field_b)s, %(field_c)s)',
    data
)

data can be your list of dicts. data可以是您的字典列表。

You can use executemany in PSQL to insert multiple records at once as follows: 您可以在PSQL中使用executemany一次插入多个记录,如下所示:

conn=psycopg2.connect("dbname='db_name' user='db_user' password='db_pass'")

data = [{"col1":"data11", "col2":"data21"},
            {"col1":"data12", "col2":"data22"},
            {"col1":"data13", "col2":"data23"}]


cur = conn.cursor()
cur.executemany("""INSERT INTO bar(col1,col2) VALUES (%(col1)s, %(col2)s)""", data)

If you do not have dict structure, you need to make sure that data is in correct sequences of your columns in your table as : 如果您没有dict结构,则需要确保数据按表中列的正确顺序排列:

cur.executemany(
    """INSERT INTO bar(col1,col2) 
       VALUES (%s,%s)""", data)

and data should be format : 并且数据应采用以下格式:

data = [['data11', 'data21'], ['data12', 'data22']]

Is that what you are trying to do? 那是你想做的吗? Let me know, it works. 让我知道,它有效。

From the docs : 文档

The current implementation of executemany() is (using an extremely charitable understatement) not particularly performing. executemany()的当前实现(使用非常慈善的低调executemany() )并不是特别有效。

As per this thread, You should be using cursor.copy_from or extras.execute_values which perform single, bulk inserts. 按照线程,您应该使用cursor.copy_fromextras.execute_values来执行单个批量插入。

copy_from

data = [{'col1': 1, 'col2': 2}, {'col1': 3, 'col2': 4}, {'col1': 5, 'col2':6}]
f = IteratorFile(("{0}\t{1}".format(k['col1'], k['col2']) for k in data.keys()))
cursor.copy_from(f, 'table_name', columns=('col1', 'col2'))

Please see here for a benchmark test compared to cursor.execute . 请参见此处 ,以获取与cursor.execute相比的基准测试。

execute_values

data = [{'col1': 1, 'col2': 2}, {'col1': 3, 'col2': 4}, {'col1': 5, 'col2':6}]
insert_query = 'insert into table_name (a, b) values %s'
psycopg2.extras.execute_values(
    cursor, insert_query, [tuple(d) for d in data]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM