Junior — Middle
Describe your experience working with databases and their use in projects.
sobes.tech AI
Answer from AI
In Python projects, I used various databases, including relational ones (PostgreSQL, MySQL) and NoSQL (MongoDB). For working with relational databases, I employed ORM libraries such as SQLAlchemy and Django ORM, which simplified database interaction and allowed for more readable and maintainable code.
Example of using SQLAlchemy to create and query data:
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:///example.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# Adding a user
new_user = User(name='Ivan')
session.add(new_user)
session.commit()
# Querying users
users = session.query(User).all()
for user in users:
print(user.name)
In NoSQL projects, I used PyMongo to interact with MongoDB, which allowed flexible storage and processing of unstructured data.
I also have experience optimizing queries, creating indexes, and designing database schemas to improve performance and ease of maintenance.