Sobes.tech
Junior — Middle

Do you have experience working with the synchronous version of SQLAlchemy?

sobes.tech AI

Answer from AI

Yes, the synchronous version of SQLAlchemy is the classic way of working with a database in Python, where operations are performed sequentially and block the thread until the query is complete. In synchronous SQLAlchemy, ORM with sessions is typically used to manage transactions and database queries.

A simple example of a query using synchronous SQLAlchemy:

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

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

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

# Adding a new user
new_user = User(name='Alice')
session.add(new_user)
session.commit()

# Querying users
users = session.query(User).filter_by(name='Alice').all()
for user in users:
    print(user.id, user.name)

This approach is simple and widely used, especially in projects without asynchronous code.

Do you have experience working with the synchronous… - sobes.tech