SqlAlchemy function insert items in batches

Python -- Posted on June 9, 2024

SqlAlchemy function to insert items in batches

              
                from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import declarative_base,sessionmaker


Base = declarative_base()

engine = create_engine('sqlite:///example.db')
Session = sessionmaker(bind=engine)
session = Session()





class Product(Base):
    __tablename__ = 'products'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    price = Column(Integer)



def insert_in_batches(session, model, data, batch_size=100):
    total_records = len(data)
    for i in range(0, total_records, batch_size):
        batch = data[i:i + batch_size]
        session.bulk_insert_mappings(model, batch)
        session.commit()
        print(f"Inserted batch {i // batch_size + 1}/{(total_records - 1) // batch_size + 1}")


products = [
    {"name": "Product A", "price": 10},
    {"name": "Product B", "price": 20},
    {"name": "Product C", "price": 30},
    # Assume there are many more products
]

# Insert the products in batches of 100

insert_in_batches(session, Product, products, batch_size=100)
                  
   
            

Related Posts