Junior — Middle
Do you have experience working with the SQLAlchemy library for database operations in Python?
sobes.tech AI
Answer from AI
Yes, I have experience working with SQLAlchemy. It is a popular ORM library for Python that allows working with databases at the object level rather than directly through SQL queries.
Example of using SQLAlchemy to define a model and perform a simple query:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
engine = create_engine('sqlite:///:memory:')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# Adding a 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)
SQLAlchemy simplifies working with databases, ensures portability, and helps avoid errors related to manual SQL writing.