db.py
This file
Path
examples/tutorial/flaskr/db.py
54 lines · 1.2 KiB · Python
Last changed by
Merge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
History of this fileTake it
The walk stopped after 200 commits: lines older than 70b4509 point at it.
Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 1–3why, one step back1import sqlite323import clickMerge pull request #3246 from pallets/reorder-importsDavid Lordbefore adoption3db4697
lines 4–5why, one step back4from flask import current_app5from flask import gMerge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 6–13why, one step back6from flask.cli import with_appcontext789def get_db():10 """Connect to the application's configured database. The connection11 is unique for each request and will be reused if this is called12 again.13 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 14why, one step back14 if "db" not in g:Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
line 15why, one step back15 g.db = sqlite3.connect(Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 16why, one step back16 current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPESMerge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 17–26why, one step back17 )18 g.db.row_factory = sqlite3.Row1920 return g.db212223def close_db(e=None):24 """If this request connected to the database, close the25 connection.26 """Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 27why, one step back27 db = g.pop("db", None)Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 28–36why, one step back2829 if db is not None:30 db.close()313233def init_db():34 """Clear existing data and create new tables."""35 db = get_db()36Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
lines 37–38why, one step back37 with current_app.open_resource("schema.sql") as f:38 db.executescript(f.read().decode("utf8"))Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 39–40why, one step back3940Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 41why, one step back41@click.command("init-db")Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 42–45why, one step back42@with_appcontext43def init_db_command():44 """Clear existing data and create new tables."""45 init_db()Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
line 46why, one step back46 click.echo("Initialized the database.")Merge pull request #2962 from garenchan/devDavid Lordbefore adoption70b4509
lines 47–54why, one step back474849def init_app(app):50 """Register database functions with the Flask app. This is called by51 the application factory.52 """53 app.teardown_appcontext(close_db)54 app.cli.add_command(init_db_command)