数据库
首页 > 数据库> > postgresql python 批量插入

postgresql python 批量插入

作者:互联网

import psycopg2
from psycopg2 import extras as ex

conn = psycopg2.connect(database="test", user="postgres", password="db12345678", host="localhost", port="5432")
cursor = conn.cursor()

# values 后面直接%s
sql = '''INSERT INTO "public"."user"("id", "name") values %s'''
datalist = []
# 这里做一个循环, 循环加入数据

# 行数据是以元组的形式存放
datalist.append((3, '1zhangsan'))
datalist.append((2, '2lisi'))

ex.execute_values(cursor, sql, datalist, page_size=1000)
conn.commit()  #需要手动提交

标签:postgresql,批量,python,psycopg2,cursor,datalist,values,ex,conn
来源: https://blog.csdn.net/qq_34874784/article/details/119040409