SQLAlchemy Setup
To store data persistently, you need a database. SQLite is a lightweight, file‑based database perfect for learning. SQLAlchemy is an Object Relational Mapper (ORM) that lets you interact with the database using Python classes instead of raw SQL.
Installing Flask‑SQLAlchemy
pip install flask-sqlalchemyConfiguring the Database
In your Flask app (`app.py`), add the following configuration:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)Creating the Database File
After defining your models (next chapter), create the database by running the Python shell:
from app import db
db.create_all()This creates a file named `site.db` in your project folder.Two Minute Drill
- SQLite is a file‑based database, no separate server needed.
- Flask‑SQLAlchemy is the ORM for Flask.
- Set `SQLALCHEMY_DATABASE_URI` to `sqlite:///filename.db`.
- `db.create_all()` creates tables from your models.
Need more clarification?
Drop us an email at career@quipoinfotech.com
