What is SQLAlchemy and how is it used in Flask?
Answer
SQLAlchemy is Python's most popular ORM (Object-Relational Mapper). Flask-SQLAlchemy integrates it with Flask, providing a convenient setup. Install: pip install flask-sqlalchemy. Configure: app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'; db = SQLAlchemy(app). Define a model: class User(db.Model): id = db.Column(db.Integer, primary_key=True); name = db.Column(db.String(100), nullable=False). Create tables: db.create_all(). Query: User.query.all(), User.query.filter_by(name='Alice').first(). Insert: db.session.add(user); db.session.commit(). Flask-SQLAlchemy automatically handles connection pooling, session lifecycle tied to the request context, and supports multiple databases.
Previous
What is Flask's application context and request context?
Next
How does FastAPI handle request validation and error responses?