相关疑难解决方法(0)

使用SQLAlchemy批量上传

我正在使用SQLAlchemy 1.1.0b将大量数据批量上传到PostgreSQL中,并且遇到了重复的关键错误。

from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.automap import automap_base

import pg

engine = create_engine("postgresql+pygresql://" + uname + ":" + passw + "@" + url)

# reflectively load the database.
metadata = MetaData()
metadata.reflect(bind=engine)
session = sessionmaker(autocommit=True, autoflush=True)
session.configure(bind=engine)
session = session()
base = automap_base(metadata=metadata)
base.prepare(engine, reflect=True)

table_name = "arbitrary_table_name" # this will always be arbitrary
mapped_table = getattr(base.classses, table_name)
# col and col2 exist in the table.
chunks = [[{"col":"val"},{"col2":"val2"}],[{"col":"val"},{"col2":"val3"}]]

for chunk in chunks:
    session.bulk_insert_mappings(mapped_table, …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy upsert

7
推荐指数
1
解决办法
6224
查看次数

标签 统计

postgresql ×1

python ×1

sqlalchemy ×1

upsert ×1